Python | Odstraňte z řetězce interpunkci
Mnohokrát při práci s Python řetězce , máme problém, ve kterém potřebujeme odstranit určité znaky z řetězců. To může mít aplikace v předzpracování dat v Krajta .
Příklad
Input: 'Gfg, is best: for ! Geeks ;' Output: Gfg is best for Geeks Explanation: Here we can observe the difference between input and output we removed all the punctuation from the input and the ways to this is listed below to do that.
Způsoby odstranění interpunkce z řetězce
Existuje mnoho způsobů, jak odstranit interpunkci z řetězce, ale ty hlavní jsou uvedeny níže. Pojďme je tedy prozkoumat jeden po druhém. Níže jsou uvedeny metody, které pokryjeme v tomto článku:
- Odstraňte interpunkci z řetězce pomocí funkce Translate
- Odstraňte interpunkci z řetězce pomocí smyčky Python
- Odstraňte čárku z řetězce pomocí smyčky Python
- Odstraňte interpunkci z řetězce pomocí regulárního výrazu
- Používá se smyčka for, řetězec interpunkce, nikoli operátor
- Odstranění interpunkce z řetězce pomocí filtru()
- Pomocí metody replace()
Odstraňte interpunkci z řetězce pomocí funkce Translate
První dva argumenty pro řetězec.přeložit metoda jsou prázdné řetězce a třetí vstup je a Seznam Pythonu interpunkce, která by měla být odstraněna. To dává metodě Pythonu pokyn k odstranění interpunkce z řetězce. Toto je jeden z nejlepší způsoby, jak odstranit interpunkci z řetězce .
Python3
import> string> test_str> => 'Gfg, is best: for ! Geeks ;'> test_str> => test_str.translate> > (> str> .maketrans('> ', '> ', string.punctuation))> print> (test_str)> |
Výstup:
Gfg is best for Geeks
Odstraňte interpunkci z řetězce pomocí smyčky Python
Toto je způsob, jakým lze tento úkol provést hrubou silou. V tomto případě zkontrolujeme interpunkci pomocí nezpracovaného řetězce, který obsahuje interpunkční znaménka, a poté vytvoříme řetězec, který tyto interpunkce odstraní.
Python3
# initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # initializing punctuations string> punc> => '''!()-[]{};:'',./?@#$%^&*_~'''> # Removing punctuations in string> # Using loop + punctuation string> for> ele> in> test_str:> > if> ele> in> punc:> > test_str> => test_str.replace(ele, '')> # printing result> print> (> 'The string after punctuation filter : '> +> test_str)> |
Výstup:
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časová složitost: Na)
Pomocný prostor: O(n), kde n je počet znaků v řetězci.
Odstraňte čárku z řetězce pomocí smyčky Python
Toto je brutální způsob, jakým lze tento úkol provést. V tomto zkontrolujeme čárku pomocí nezpracovaného řetězce, který obsahuje čárky, a poté vytvoříme řetězec, který tyto čárky odstraní.
Python3
def> remove_commas(string):> > result> => ''> > for> char> in> string:> > if> char !> => ','> :> > result> +> => char> > return> result> > input_string> => 'GFG, is, the, best.'> output_string> => remove_commas(input_string)> print> (output_string)> |
Výstup:
GFG is the best
Odstraňte interpunkci z řetězce pomocí regulárního výrazu
Část nahrazení interpunkce lze také provést pomocí regulární výraz . V tomto nahradíme veškerou interpunkci prázdným řetězcem pomocí určitého regulárního výrazu.
Python3
import> re> # initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # Removing punctuations in string> # Using regex> res> => re.sub(r> '[^ws]'> , '', test_str)> # printing result> print> (> 'The string after punctuation filter : '> +> res)> |
Výstup :
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Používá se smyčka for, řetězec interpunkce, nikoli operátor
Zde uvidíme Odstranění interpunkce v řetězci pomocí smyčky + řetězec interpunkce.
Python3
# initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # initializing punctuations string> punc> => '''!()-[]{};:'',./?@#$%^&*_~'''> res> => for> ele> in> test_str:> > if> ele> not> in> punc:> > res> +> => ele> > # printing result> print> (> 'The string after punctuation filter : '> +> res)> |
Výstup
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časová a prostorová složitost pro všechny metody je stejná:
Časová složitost: Na)
Pomocný prostor: Na)
Odstranění interpunkce z řetězce pomocí filtru()
Metoda filter() filtruje prvky sekvence na základě dané podmínky.
V tomto případě můžeme k odfiltrování interpunkčních znaků použít metodu filter() a funkci lambda.
Python3
def> remove_punctuation(test_str):> # Using filter() and lambda function to filter out punctuation characters> > result> => ''.join(> filter> (> lambda> x: x.isalpha()> or> x.isdigit()> or> x.isspace(), test_str))> > return> result> test_str> => 'Gfg, is best : for ! Geeks ;'> print> (> 'The original string is : '> +> test_str)> result> => remove_punctuation(test_str)> print> (> 'The string after punctuation filter : '> +> result)> #This code is contributed by Edula Vinay Kumar Reddy> |
Výstup
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časová složitost: Na)
Pomocný prostor: Na)
Odstranění interpunkce z řetězce pomocí metody replace().
Importujte modul řetězce, inicializujte vstupní řetězec a vytiskněte původní řetězec. Projděte každý interpunkční znak v řetězcové interpunkční konstantě poté, co použije metodu replace() k odstranění každého interpunkčního znaku ze vstupního řetězce. a poté vytiskněte výsledný řetězec po odstranění interpunkce.
Python3
import> string> # initializing string> test_str> => 'Gfg, is best : for ! Geeks ;'> # printing original string> print> (> 'The original string is : '> +> test_str)> # Removing punctuations using replace() method> for> punctuation> in> string.punctuation:> > test_str> => test_str.replace(punctuation, '')> # printing result> print> (> 'The string after punctuation filter : '> +> test_str)> |
Výstup
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Analýza časové složitosti: O(délka(řetězec.interpunkce) *délka(test_str)) jak smyčka for iteruje všechny interpunkční znaky v konstantě string.interpunkce, což trvá O(len(řetězec.interpunkce)) čas.
Analýza pomocného prostoru: O(1) . Protože je vstupní řetězec upraven na místě, není pro uložení výsledku potřeba žádné místo navíc.