Python | Odstráňte interpunkciu z reťazca

Mnohokrát pri práci s Python reťazce , máme problém, v ktorom potrebujeme odstrániť určité znaky z reťazcov. To môže mať aplikácie v predspracovaní údajov v Python .

Prí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. 

Spôsoby, ako odstrániť interpunkciu z reťazca

Existuje mnoho spôsobov, ako odstrániť interpunkciu z reťazca, ale hlavné sú uvedené nižšie. Poďme ich teda preskúmať jeden po druhom. Nižšie sú uvedené metódy, ktorým sa budeme venovať v tomto článku:

  • Odstráňte interpunkciu z reťazca pomocou funkcie Translate
  • Odstráňte interpunkciu z reťazca pomocou slučky Python
  • Odstráňte čiarku z reťazca pomocou slučky Python
  • Odstráňte interpunkciu z reťazca pomocou regulárneho výrazu
  • Používa sa slučka for, reťazec interpunkcie a nie operátor
  • Odstránenie interpunkcie z reťazca pomocou filtra ()
  • Pomocou metódy replace()

Odstráňte interpunkciu z reťazca pomocou funkcie Translate

Prvé dva argumenty pre reťazec.preložiť metóda sú prázdne reťazce a tretí vstup je a Zoznam Python interpunkcie, ktorá by sa mala odstrániť. Toto prikazuje metóde Python odstrániť interpunkciu z reťazca. Toto je jeden z najlepšie spôsoby, ako odstrániť interpunkciu z reťazca .

Python3




import> string> test_str> => 'Gfg, is best: for ! Geeks ;'> test_str> => test_str.translate> > (> str> .maketrans('> ', '> ', string.punctuation))> print> (test_str)>

Výkon:

Gfg is best for Geeks 

Odstráňte interpunkciu z reťazca pomocou slučky Python

Toto je spôsob hrubej sily, ktorým možno túto úlohu vykonať. V tomto prípade kontrolujeme interpunkciu pomocou nespracovaného reťazca, ktorý obsahuje interpunkčné znamienka, a potom vytvoríme reťazec, ktorý tieto interpunkčné znamienka odstráni.

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ýkon:

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 

Časová zložitosť: O(n)
Pomocný priestor: O(n), kde n je počet znakov v reťazci.

Odstráňte čiarku z reťazca pomocou slučky Python

Toto je brutálny spôsob, akým možno túto úlohu vykonať. V tomto prípade skontrolujeme čiarku pomocou surového reťazca, ktorý obsahuje čiarky, a potom vytvoríme reťazec, ktorý tieto čiarky odstráni.

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ýkon:

GFG is the best 

Odstráňte interpunkciu z reťazca pomocou regulárneho výrazu

Časť nahradenia interpunkcie možno vykonať aj pomocou regulárny výraz . V tomto nahradíme všetku interpunkciu prázdnym reťazcom pomocou určitého regulárneho 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ýkon :

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 

Používa sa slučka for, reťazec interpunkcie a nie operátor

Tu uvidíme Odstránenie interpunkčných znamienok v reťazci pomocou slučky + interpunkčného reťazca.

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ýkon

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 

Časová a priestorová zložitosť pre všetky metódy je rovnaká:

Časová zložitosť: O(n)
Pomocný priestor: O(n)

Odstránenie interpunkcie z reťazca pomocou filtra ()

Metóda filter() filtruje prvky sekvencie na základe danej podmienky.
V tomto prípade môžeme použiť metódu filter() a funkciu lambda na odfiltrovanie interpunkčných znakov.

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ýkon

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 

Časová zložitosť: O(n)
Pomocný priestor: O(n)

Odstránenie interpunkcie z reťazca pomocou metódy replace().

Importujte modul reťazca, potom inicializujte vstupný reťazec a vytlačte pôvodný reťazec. Prechádzajte cez každý interpunkčný znak v interpunkčnej konštante reťazca potom, čo použije metódu replace() na odstránenie každého interpunkčného znaku zo vstupného reťazca. a potom vytlačte výsledný reťazec po odstránení interpunkcie.

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ýkon

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 

Analýza časovej zložitosti: O(len(reťazec.interpunkcia) * dĺžka(test_str)) ako cyklus for iteruje cez všetky interpunkčné znaky v reťazci.interpunkčná konštanta, ktorá trvá O(len(reťazec.interpunkcia)) čas.

Analýza pomocného priestoru: O(1) . Pretože vstupný reťazec je upravený na mieste, nie je potrebný žiadny ďalší priestor na uloženie výsledku.