Python | Odstranite ločila iz niza
Velikokrat med delom z Python nizi , imamo težavo, pri kateri moramo odstraniti določene znake iz nizov. To ima lahko aplikacije v predobdelavi podatkov v Python .
Primer
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.
Načini odstranjevanja ločil iz niza
Obstaja veliko načinov za odstranitev ločil iz niza, vendar so glavni navedeni spodaj. Zato jih raziščimo enega za drugim. Spodaj so metode, ki jih bomo obravnavali v tem članku:
- Odstranite ločila iz niza s prevajalnikom
- Odstranite ločila iz niza z zanko Python
- Odstranite vejico iz niza z zanko Python
- Odstranite ločila iz niza z regularnim izrazom
- Uporaba zanke for, niza ločil in ne v operatorju
- Odstranjevanje ločil iz niza s filter()
- Uporaba metode replace().
Odstranite ločila iz niza s prevajalnikom
Prva dva argumenta za string.translate metoda so prazni nizi, tretji vhod pa je a Seznam Python ločil, ki jih je treba odstraniti. To ukaže metodi Python, da odstrani ločila iz niza. To je eden od najboljši načini za odstranjevanje ločil iz niza .
Python3
import> string> test_str> => 'Gfg, is best: for ! Geeks ;'> test_str> => test_str.translate> > (> str> .maketrans('> ', '> ', string.punctuation))> print> (test_str)> |
Izhod:
Gfg is best for Geeks
Odstranite ločila iz niza z zanko Python
To je brutalni način, na katerega je mogoče izvesti to nalogo. Pri tem preverimo ločila z neobdelanim nizom, ki vsebuje ločila, nato pa sestavimo niz, ki odstrani ta ločila.
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)> |
Izhod:
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časovna zahtevnost: O(n)
Pomožni prostor: O(n), kjer je n število znakov v nizu.
Odstranite vejico iz niza z zanko Python
To je surov način, na katerega je mogoče opraviti to nalogo. Pri tem vejico preverimo z neobdelanim nizom, ki vsebuje vejice, nato pa sestavimo niz, ki odstrani te vejice.
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)> |
Izhod:
GFG is the best
Odstranite ločila iz niza z regularnim izrazom
Del zamenjave ločil lahko izvedete tudi z uporabo regex . Pri tem zamenjamo vsa ločila s praznim nizom z uporabo določenega regularnega izraza.
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)> |
Izhod:
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Uporaba zanke for, niza ločil in ne v operatorju
Tukaj bomo videli Odstranjevanje ločil v nizu z uporabo zanke + niz ločil.
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)> |
Izhod
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časovna in prostorska kompleksnost za vse metode sta enaki:
Časovna zahtevnost: O(n)
Pomožni prostor: O(n)
Odstranjevanje ločil iz niza s filter()
Metoda filter() filtrira elemente zaporedja na podlagi danega pogoja.
V tem primeru lahko uporabimo metodo filter() in funkcijo lambda za filtriranje ločil.
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> |
Izhod
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Časovna zahtevnost: O(n)
Pomožni prostor: O(n)
Odstranjevanje ločil iz niza z uporabo metode replace().
Uvozite modul niza, nato inicializirajte vhodni niz in natisnite izvirni niz. Preglejte vsako ločilo v konstanti ločila niza, potem ko ta uporabi metodo replace() za odstranitev vsakega ločila iz vhodnega niza. in nato natisnite dobljeni niz po odstranitvi ločil.
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)> |
Izhod
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Analiza časovne kompleksnosti: O(len(string.punctuation) * len(test_str)) saj zanka for ponavlja vsa ločila v konstanti string.punctuation, kar traja O(len(string.punctuation)) časa.
Analiza pomožnega prostora: O(1) . Ker je vhodni niz spremenjen na mestu, zato za shranjevanje rezultata ni potreben dodaten prostor.