Pythona | Usuń znaki interpunkcyjne z ciągu
Wiele razy podczas pracy z Ciągi Pythona , mamy problem polegający na tym, że musimy usunąć pewne znaki z ciągów. Może to mieć zastosowanie w przetwarzaniu wstępnym danych w plikach Pyton .
Przykład
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.
Sposoby usuwania znaków interpunkcyjnych z ciągu znaków
Istnieje wiele sposobów usunięcia znaków interpunkcyjnych z ciągu, ale najważniejsze z nich są wymienione poniżej. Przeanalizujmy je więc jeden po drugim. Poniżej znajdują się metody, które omówimy w tym artykule:
- Usuń znaki interpunkcyjne z ciągu za pomocą narzędzia Translate
- Usuń znaki interpunkcyjne z ciągu za pomocą pętli Pythona
- Usuń przecinek z ciągu za pomocą pętli Pythona
- Usuń znaki interpunkcyjne z ciągu za pomocą wyrażenia regularnego
- Używanie pętli for, łańcucha interpunkcyjnego, a nie operatora in
- Usuwanie znaków interpunkcyjnych z ciągu za pomocą filter()
- Korzystanie z metody zamiany().
Usuń znaki interpunkcyjne z ciągu za pomocą narzędzia Translate
Pierwsze dwa argumenty za ciąg.tłumacz metoda to puste ciągi znaków, a trzecim wejściem jest a Lista Pythona znaków interpunkcyjnych, które należy usunąć. To instruuje metodę Pythona, aby wyeliminować znaki interpunkcyjne z ciągu. To jest jeden z najlepsze sposoby na usunięcie znaków interpunkcyjnych z ciągu .
Python3
import> string> test_str> => 'Gfg, is best: for ! Geeks ;'> test_str> => test_str.translate> > (> str> .maketrans('> ', '> ', string.punctuation))> print> (test_str)> |
Wyjście:
Gfg is best for Geeks
Usuń znaki interpunkcyjne z ciągu za pomocą pętli Pythona
To zadanie można wykonać metodą brute-force. W tym przypadku sprawdzamy interpunkcję za pomocą surowego ciągu znaków zawierającego znaki interpunkcyjne, a następnie konstruujemy ciąg znaków usuwający te znaki interpunkcyjne.
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)> |
Wyjście:
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(n), gdzie n jest liczbą znaków w ciągu.
Usuń przecinek z ciągu za pomocą pętli Pythona
To jest brutalny sposób, w jaki można wykonać to zadanie. W tym przypadku sprawdzamy obecność przecinka za pomocą surowego ciągu znaków zawierającego przecinki, a następnie konstruujemy ciąg znaków usuwający te przecinki.
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)> |
Wyjście:
GFG is the best
Usuń znaki interpunkcyjne z ciągu za pomocą wyrażenia regularnego
Część zastępującą znaki interpunkcyjne można również wykonać za pomocą wyrażenie regularne . W tym przypadku zastępujemy wszystkie znaki interpunkcyjne pustym ciągiem znaków, używając określonego wyrażenia regularnego.
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)> |
Wyjście :
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Używanie pętli for, łańcucha interpunkcyjnego, a nie operatora in
Tutaj zobaczymy usuwanie znaków interpunkcyjnych w ciągu za pomocą pętli + ciągu znaków interpunkcyjnych.
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)> |
Wyjście
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Złożoność czasowo-przestrzenna dla wszystkich metod jest taka sama:
Złożoność czasowa: NA)
Przestrzeń pomocnicza: NA)
Usuwanie znaków interpunkcyjnych z ciągu za pomocą filter()
Metoda filter() filtruje elementy sekwencji na podstawie danego warunku.
W tym przypadku możemy użyć metody filter() i funkcji lambda do odfiltrowania znaków interpunkcyjnych.
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> |
Wyjście
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Złożoność czasowa: NA)
Przestrzeń pomocnicza: NA)
Usuwanie znaków interpunkcyjnych z ciągu za pomocą metody zamiany().
Zaimportuj moduł string, a następnie zainicjuj ciąg wejściowy i wydrukuj oryginalny ciąg. Wykonaj pętlę przez każdy znak interpunkcyjny w stałej interpunkcyjnej ciągu po użyciu metody zamiany() w celu usunięcia każdego znaku interpunkcyjnego z ciągu wejściowego. a następnie wydrukuj wynikowy ciąg po usunięciu znaków interpunkcyjnych.
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)> |
Wyjście
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Analiza złożoności czasu: O(len(string.interpunkcja) * len(test_str)) podczas gdy pętla for iteruje po wszystkich znakach interpunkcyjnych w stałej string.interpunkcja, co zajmuje czas O(len(string.interpunkcja)).
Analiza przestrzeni pomocniczej: O(1) . Ponieważ ciąg wejściowy jest modyfikowany lokalnie, nie jest wymagana dodatkowa przestrzeń do przechowywania wyniku.