パイソン |文字列から句読点を削除する

一緒に仕事をしていると何度も Python 文字列 、文字列から特定の文字を削除する必要があるという問題があります。これにより、データの前処理にアプリケーションを使用できます。 パイソン

  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. 

文字列から句読点を削除する方法

文字列から句読点を削除する方法はたくさんありますが、主な方法を以下に示します。それでは、それらを 1 つずつ調べてみましょう。この記事で説明する方法は次のとおりです。

  • Translate を使用して文字列から句読点を削除する
  • Python ループを使用して文字列から句読点を削除する
  • Python ループを使用して文字列からカンマを削除する
  • 正規表現を使用して文字列から句読点を削除する
  • for ループ、句読点文字列、not in 演算子の使用
  • filter() を使用して文字列から句読点を削除する
  • replace() メソッドの使用

Translate を使用して文字列から句読点を削除する

最初の 2 つの引数は、 文字列.翻訳 メソッドは空の文字列で、3 番目の入力は Python リスト 削除する必要がある句読点。これは、文字列から句読点を削除するように Python メソッドに指示します。これはそのうちの 1 つです 文字列から句読点を削除する最良の方法

Python3




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

出力:

Gfg is best for Geeks 

Python ループを使用して文字列から句読点を削除する

これは、このタスクを実行できる総当たりの方法です。ここでは、句読点を含む生の文字列を使用して句読点をチェックし、それらの句読点を削除した文字列を構築します。

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)>

出力:

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

時間計算量: の上)
補助スペース: O(n)。n は文字列内の文字数です。

Python ループを使用して文字列からカンマを削除する

これは、このタスクを実行できる乱暴な方法です。ここでは、カンマを含む生の文字列を使用してカンマをチェックし、それらのカンマを削除した文字列を構築します。

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)>

出力:

GFG is the best 

正規表現を使用して文字列から句読点を削除する

句読点の置換の部分は、次の方法でも実行できます。 正規表現 。ここでは、特定の正規表現を使用してすべての句読点を空の文字列に置き換えます。

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)>

出力:

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

for ループ、句読点文字列、not in 演算子の使用

ここでは、ループ + 句読点文字列を使用して文字列内の句読点を削除する方法を見ていきます。

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)>

出力

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

すべてのメソッドの時間と空間の複雑さは同じです。

時間計算量: の上)
補助スペース: の上)

filter() を使用して文字列から句読点を削除する

filter() メソッドは、指定された条件に基づいてシーケンスの要素をフィルター処理します。
この場合、filter() メソッドとラムダ関数を使用して句読点文字を除外できます。

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>

出力

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

時間計算量: の上)
補助スペース: の上)

replace() メソッドを使用して文字列から句読点を削除する

文字列モジュールをインポートし、入力文字列を初期化して元の文字列を出力します。 replace() メソッドを使用して入力文字列から各句読点を削除した後、文字列句読点定数内の各句読点をループします。句読点を削除した後、結果の文字列を出力します。

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)>

出力

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

時間計算量分析: O(len(string.punctuation) * len(test_str)) for ループは string.punctuation 定数内のすべての句読点文字を反復処理するため、O(len(string.punctuation)) 時間がかかります。

補助スペース分析: O(1) 。入力文字列はその場で変更されるため、結果を保存するための追加のスペースは必要ありません。