logo

パイソン |文字列から複数の文字を分割する

Python では、文字列はテキスト データの保存と操作に使用される基本的なデータ型です。文字列を多数の文字に分割することは、Python で頻繁に行われるテキスト処理アクティビティです。コーディングを行ったり、プログラミング スキルを即興で向上させたりする際に、使いたい場面にきっとたくさん遭遇したはずです。 スプリット() Python では、1 つの文字のみで分割するのではなく、一度に複数の区切り文字で分割します。この記事では、さまざまなアプローチについて説明します。 Python 文字列 複数の区切り文字を分割します。

  Input:   'GeeksForGeeks, is an-awesome! website'   Output:   ['GeeksForGeeks, ', 'is', 'an-awesome!', 'website']   Explanation:   In This, we are splitting the multiple delimiters from the string.>

パイソン , さまざまな方法を使用して、文字列から複数の区切り記号を分割できます。これらのメソッドを使用すると、Python の文字列から個々の文字を簡単に分割して操作できます。



Split 関数を使用して複数の区切り文字で文字列を分割する

Python では、split() を使用して文字列から複数の文字を分割できます。ここでは、各区切り文字を反復処理し、次のメソッドを使用して文字列を分割します。 スプリット()関数。 分割後、結果のリストをスペースで結合します。 join() 関数 そして、変更された文字列を空白に基づいて分割して、必要な文字列のリストを取得します。

Python3








string>=> 'GeeksForGeeks, | is an-awesome! website'> delimiters>=> [>','>,>'|'>,>';'>,>'!'>]> for> delimiter>in> delimiters:> >string>=> ' '>.join(string.split(delimiter))> result>=> string.split()> print>(result)>

>

Linux用のgzip

>

出力

['GeeksForGeeks', 'is', 'an-awesome', 'website']>

Python replace() を使用して複数の文字で分割する

Python では、次を使用して文字列から複数の文字を分割できます。 交換する()。 これは非常に斬新な分割方法です。正規表現を使用しないため非効率ですが、それでも試してみる価値はあります。分割したい文字がわかっている場合は、それらをスペースに置き換えて、次を使用します。 スプリット()

Python3




data>=> 'Let's_try, this now'> # printing original string> print>(>'The original string is : '> +> data)> # Using replace() and split()> # Splitting characters in String> res>=> data.replace(>'_'>,>' '>).replace(>', '>,>' '>).split()> print>(>'The list after performing split functionality : '> +> str>(res))>

>

>

出力

The original string is : Let's_try, this now The list after performing split functionality : ['Let's', 'try', 'this', 'now']>

Python Re.split() を使用して複数の文字で分割する

Python では、次を使用して文字列から複数の文字を分割できます。 再分割()。 これは、複数の文字を一度に分割する最も効率的で一般的に使用される方法です。これを行うために regex (正規表現) を使用します。

Python3




動的プログラミング

import> re> data>=> 'techcodeview.com, is_an-awesome ! website'> print>(>'The original string is : '> +> data)> # Using re.split()> # Splitting characters in String> res>=> re.split(>', |_|-|!'>, data)> print>(>'The list after performing split functionality : '> +> str>(res))>

>

Java ロングから int

>

出力

The original string is : techcodeview.com, is_an-awesome ! website The list after performing split functionality : ['techcodeview.com', 'is', 'an', 'awesome ', ' website']>

re.split(‘, |_|-|!’, data) という行は、変数データを文字で分割するように Python に指示します。 または _ または または 。象徴 | またはを表します。正規表現には、特殊記号として扱われ、異なる機能を持つ記号がいくつかあります。このようなシンボルで分割したい場合は、次のコマンドを使用してエスケープする必要があります。 (バックスラッシュ) 特殊文字の前後にスペースが 1 つ必要です。

使用する前にエスケープする必要がある特殊文字のリスト:

.  + * ? [ ^ ] $ ( ) { } = | :>

例: このコードでは、Python の文字列から文字を分割するために resplit () を使用しています。

Python3




import> re> newData1>=> 'techcodeview.com, is_an-awesome ! app + too'> # To split '+' with one espace before and after '+' symbol and use backslash> print>(re.split(>', |_|-|!|+'>, newData1))> newData2>=> 'techcodeview.com, is_an-awesome ! app+too'> # To split '+' without one espace before and after '+' symbol and use backslash> print>(re.split(>', |_|-|!|+'>, newData2))>

>

>

出力

['techcodeview.com', ' is', 'an', 'awesome', ' app', 'too']>

注記: 正規表現について詳しく知るには ここをクリック

re.findall() を使用して複数の区切り文字で文字列を分割する

Python では、refindall() を使用して文字列から複数の文字を分割できます。これはもう少し難解な形式ですが、時間を節約できます。また、上記のような正規表現を使用しますが、代わりに 。スプリット() メソッドでは、と呼ばれるメソッドを使用します。 findall() 。このメソッドは、一致するすべてのインスタンスを検索し、それぞれをリストで返します。この分割方法は、分割したい文字が正確にわからない場合に最適です。

Python3

10億は何百万ですか




import> re> data>=> 'This, is - another : example?!'> print>(>'The original string is : '> +> data)> # Using re.findall()> # Splitting characters in String> res>=> re.findall(r>'[w']+'>, data)> print>(>'The list after performing split functionality : '> +> str>(res))>

>

>

出力

The original string is : This, is - another : example?! The list after performing split functionality : ['This', 'is', 'another', 'example']>

ここで、キーワード [w']+ は、1 つ以上のアルファベットまたはアンダースコア (_) のすべてのインスタンスを検索し、リストで返すことを示します。 注記: [w']+ はアンダースコアで分割されません( _ ) アンダースコアだけでなくアルファベットも検索します。

例: このコードでは、refindall() を使用して文字列から文字を分割しています。 パイソン。

Python3




import> re> testData>=> 'This, is - underscored _ example?!'> print>(re.findall(r>'[w']+'>, testData))>

>

>

フリップフロップ

出力

['This', 'is', 'underscored', '_', 'example']>

文字クラス

正規表現チートシート キャラクター説明について

速記文字クラス を表します
d 0 ~ 9 の任意の数字
D 0 ~ 9 の数字以外の任意の文字
任意の文字、数字、またはアンダースコア文字
文字、数字、またはアンダースコア文字ではない任意の文字
s 任意のスペース、タブ、または改行文字
S スペース、タブ、改行以外の任意の文字