与えられたリストのタスクは、以下で提供されるメソッドまたは方法を使用して、リスト内の文字列データ型の要素がリスト内に存在するかどうかをチェックするプログラムを作成することです。 パイソン 。
例:
Input: l=[1, 1.0, 'have', 'a', 'geeky', 'day']; s='geeky' Output: True Explanation: geeky is present in the input list>
Python でリスト内の文字列を検索する
この記事で説明する方法は次のとおりです。
- 使用する 演算子内
- 使用する カウント() 関数
- リスト内包表記の使用
- 使用する どれでも() 関数
- find() メソッドでの map() の使用
- 使用する オペレーター メソッド
- Try/Except と Index() の使用
- Python で re を使用する
- 使用する enumerate() 関数
- 使用する ナンピー 図書館
「in」演算子を使用してリスト内の文字列を検索する
の で 演算子は、特定の文字列/要素がリストに存在するかどうかを確認するのに便利です。ここでは、「in」演算子を含む条件文を使用して、リストに特定の文字列があるかどうかを確認し、条件演算子がそれに応じてブール値を返すコードを示します。
Python3
# assign list> l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'geeky'> # check if string is present in the list> if> s>in> l:> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
geeky is present in the list>
時間計算量: の上)
補助スペース: ○(1)
count() メソッドを使用してリスト内の文字列を検索する
の カウント() 関数は、リスト内の特定の文字列の出現をカウントするために使用されます。 Python の文字列リストで文字列のカウントが 0 より大きい場合、特定の文字列がリストに存在し、それ以外の場合、その文字列がリストに存在しないことを意味します。指定されたコードでは、条件ステートメントを使用して、特定の文字列が存在するかどうかを確認し、存在しない場合は count 関数が 0 を返します。
Python3
# assign list> l>=> [>'1'>,>1.0>,>32>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'prime'> # check if string is present in list> if> l.count(s)>>>0>:> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
prime is not present in the list>
時間計算量: O(n)
補助スペース: O(1)
リスト内包表記を使用してリスト内の文字列を検索する
リスト内包表記 タプル、文字列、配列、リストなどの他の反復可能オブジェクトから新しいリストを作成するために使用されます。反復ステートメントを式に変換するために使用されます。ここでは、リスト内で検索する必要がある文字列を格納するリストを作成し、それを新しいリストに格納します。その後、新しいリストが空かどうかを確認できます。
Python3
それぞれタイプスクリプト
# assign list> l>=> [>'hello'>,>'geek'>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'geek'> # list comprehension> compare>=> [i>for> i>in> l>if> s>in> l]> # check if string is present in list> if> len>(compare)>>>0>:> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
geek is present in the list>
時間計算量: の上)
空間の複雑さ: の上)
ジャッカル対オオカミ
any() 関数を使用してリスト内の文字列を検索する
の どれでも() 関数は、リスト内の要素の存在を確認するために使用されます。これは、文字列内のいずれかの要素が入力要素と一致する場合は、その要素がリストに存在することを出力し、そうでない場合は、要素がリストに存在しないことを出力するのと似ています。
Python3
# assign list> l>=> [>'hello'>,>'geek'>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'prime'> # check if string is present in list> if> any>(s>in> i>for> i>in> l):> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
prime is not present in the list>
時間計算量: の上)
空間の複雑さ: の上)
map() メソッドと find() メソッドを使用したリスト内の文字列の検索
ここで 地図() リストのすべてのデータ型を文字列に変換し、それらを結合して単一の文字列を作成します。 探す() 関数でその中の文字列を検索します。
Python3
l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'geeky'> nl>=>list>(>map>(>str>,l))> x>=>' '>.join(nl)> # check if string is present in the list> if> x.find(s)!>=>->1>:> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
geeky is present in the list>
時間計算量: の上)
空間の複雑さ: の上)
演算子メソッドを使用してリスト内の文字列を検索する
例 1: Operator.countOf() メソッドの使用
Python には演算子モジュールがあります cout.Of() リスト内に存在する特定の要素の出現数を返し、存在しない場合は 0 を返すメソッドです。これにより、リスト内の文字列を見つけることもできます。
Python3
import> operator as op> # assign list> l>=> [>'1'>,>'1'>,>1.0>,>32>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'prime'> # check if string is present in list> if> op.countOf(l, s):> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> #it return the number of occurence of element in the list> print>(>'The no. of occurrence of '1' in the list is:'>,op.countOf(l,>'8'>))> |
>
>出力
prime is not present in the list The no. of occurrence of '1' in the list is: 0>
時間計算量: の上)
補助スペース: ○(1)
例 2:operator.contains() の使用
指定された文字列が 演算子を使用したリスト。 を含む() 。もし の はい、表示文字列がリストに存在します。そうでない場合、表示文字列がリストに存在しません。
Python3
# assign list> import> operator> l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'geeky'> # check if string is present in the list> if> operator.contains(l, s):> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
geeky is present in the list>
時間計算量 : O(n) n – リストの長さ
補助スペース : O(1)
リスト内の文字列を検索する Try/Except と Index() の使用
使用できます 索引() リスト内の文字列の最初のインデックスを検索するメソッド。文字列がリストに存在する場合、index() メソッドは文字列の最初のインデックスを返します。それ以外の場合は、ValueError が発生します。文字列がリストに存在するかどうかを確認するには、index() メソッドを 例外を試みる ブロックし、文字列がリストに存在するかどうかを示すメッセージを出力します。
Python3
# assign list> l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> > # assign string> s>=> 'geeky'> > try>:> ># check if string is present in list> >index>=> l.index(s)> >print>(f>'{s} is present in the list at index {index}'>)> except> ValueError:> >print>(f>'{s} is not present in the list'>)> |
>
>
バッシュスリープ出力
geeky is present in the list at index 4>
時間計算量: O(n)
補助スペース: O(1)、 このメソッドでは、リストのサイズに関係なく、少数の一定サイズの変数のみが使用されるためです。
re モジュールを使用してリスト内の文字列を検索する
リスト l と文字列 s を初期化して、re モジュールをインポートします。使用 研究() リスト内の文字列 s を検索する関数 l 一致が見つかった場合は、文字列 s がリストに存在することを出力します。一致が見つからない場合は、文字列 s がリストに存在しないことを出力します。
Python3
import> re> # assign list> l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'geeky'> # check if string is present in each string in the list> for> item>in> l:> >if> isinstance>(item,>str>)>and> re.search(s, item):> >print>(f>'{s} is present in the list'>)> >break> else>:> >print>(f>'{s} is not present in the list'>)> # This code is contributed by Vinay Pinjala.> |
>
>
関係構成出力
geeky is present in the list>
時間計算量: O(n*m)、
補助スペース: O(1)
Python enumerate() 関数を使用した文字列のリストの検索
この例では、Python コードは enumerate を使用して混合型リストを反復処理し、文字列要素に文字列「geeky」が存在するかどうかを確認します。 re.search() 関数は、文字列要素内で正規表現検索を実行するために使用され、文字列「geeky」の有無をリスト内のインデックスとともに示す結果が出力されます。
Python3
import> re> # Sample list of mixed types> l>=> [>1>,>2.0>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # Assign string> s>=> 'geeky'> # Check if string is present in each string in the list using enumerate> for> index, item>in> enumerate>(l):> >if> isinstance>(item,>str>)>and> re.search(s, item):> >print>(f>'{s} is present in the list at index {index}'>)> >break> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>出力
geeky is present in the list at index 4>
時間計算量: O(n)
補助スペース: O(1)
リスト内の文字列を検索する numpyライブラリの使用
numpy モジュールをインポートし、リスト変数 l をいくつかの文字列値で初期化します。文字列変数 s を値プライムで初期化します。リスト内包表記を使用してブール値のリストを作成します。各値は、文字列 s がリスト l の対応する要素に存在するかどうかに対応します。np.any() メソッドを使用します。結果のリスト内の値のいずれかが True であるかどうかを確認します。 res の値に基づいて、文字列 s がリストに存在するかどうかを示すメッセージを出力します。
Python3
import> numpy as np> # assign list> l>=> [>'hello'>,>'geek'>,>'have'>,>'a'>,>'geeky'>,>'day'>]> # assign string> s>=> 'prime'> # check if string is present in list> res>=> np.>any>([s>in> i>for> i>in> l])> if> res:> >print>(f>'{s} is present in the list'>)> else>:> >print>(f>'{s} is not present in the list'>)> |
>
>
出力
prime is not present in the list>
時間計算量: O(nm)
補助スペース:O(nm)