How to find element in python list

author
2 minutes, 9 seconds Read

Python にはデータのコレクションを格納するためのさまざまなデータ型があります。 Python のリストはそれらの 1 つであり、リストには、数値、文字列、ブール値などのさまざまな種類のデータを含めることができます。 時には、リスト内の特定の要素を検索する必要があります。 要素は、様々な方法でPythonのリストで検索することができます。 3584>

Example-1: Find a single element in a list using ‘in’ operator

The following script shows how you can easily search any element in a list by ‘in’ operator using any loop without using the pleasure. 花の名前のリストがスクリプト内で定義され、花の名前はリスト内を検索するためのユーザーからの入力として受け取られる。

#!/usr/bin/env python3
# Define a list of flowers
flowerList =
# Take the name of the flower that you want to search in the list
flowerName = input(“Enter a flower name:”)
# Search the element using ‘in’ operator
if flowerName.FlowersName=”Flowers Name”
{flowerList=”Flowers Name”(花の名前を入力してください。)}
{flowerName=”Flowers Name”(花の名前を入力してください。)}{flowerName=”flowerName”(花の名前を入力してください。)lower() in flowerList:
# Print success message
print(“%s is found in the list” %(flowerName))
else:
# Print not found message
print(“%s is not found in the list” %(flowerName))

出力:

出力は、Daffodilがリストに存在し、Lilyがリストに存在しないことを示しています。

例2:インデックス法を使用して要素を見つける

インデックス法を使ってリスト内の特定の要素を見つけるもう1つの簡単な方法。 次のスクリプトは、リスト内の要素を検索するためのindex()メソッドの使用法を示しています。 このメソッドは、特定の要素がリストで見つかった場合は有効なインデックス位置を返します。そうでない場合は、変数に位置を格納したい場合は ValueError を生成します。 exceptブロックは、検索している要素がリストに存在しない場合、失敗メッセージを表示します。/usr/bin/env python3
try:
# Define a list of books
bookList =
# Take the name of the book that you want to search in the list
bookName = input(“Enter a book name:”)
# Search the element using index method
search_pos = int(bookList.BOOKLIST.Index)
# Search the element using index method
search_pos = int(bookList.Index)
# Search the element using index method
BOOKLIST.BOOKLIST.Index) # Print found message
print(“%s book is found in the list” %(bookName))
except(ValueError):
# Print not found message
print(“%s book is not found in the list” %(bookName))

Output:

出力は ‘Goodnight Moon’ がリストに存在し、 ‘Charlie and the Chocolate Factory’ がリストに存在しないことを示しています。

例 3: リスト内の複数のインデックスを見つける

リスト内の単一の要素を見つける方法は前の 2 例で示されています。 次のスクリプトは、別のリストの中にあるリストの全要素を検索する方法を示している。 selectedListはselectedListの要素が検索されるメインのリストである。 最初の for ループは foundList を生成するために使用され、2 番目の for ループは foundList を繰り返し、その出力を表示するために使用されます。/usr/bin/env python3
# selected personのリストを定義
selectedList =
# searching personのリストを定義
searchList =
# 空リストを定義
foundList =
# selected listからそれぞれの要素を反復
for index, sList in enumerate(selectedList)しています。
# searchListの要素とマッチさせる
if sList in searchList:
# マッチが見つかったらfoundListに値を格納する
foundList.List =
for index, sList in enumerate(selectedList):
# index, sList in searchList:
# findList.append(selectedList)
# iterate the searchList
for val in searchList:
# check the value exists in foundList or not
if val in foundList:
print(“%s is selected.”).\n” %val)
else:
print(“%s is not selected.\n” %val)

Output:

word を実行すると、以下の出力が表示されます。

例4: カスタム関数を使用して要素を検索する

リスト内の要素を複数回検索したい場合は、検索スクリプトを何度も書くのではなく、カスタム検索方法を使用するとよいでしょう。 次のスクリプトは、findElementというカスタム関数を使用して、リスト内の任意の値を検索する方法を示しています。 この関数は、リストが検索要素を含んでいれば True を、そうでなければ False を返します。/usr/bin/env python3
# Define a list of food
food =
# Take a food name from the user
search = input(‘Type your favorite food : ‘)
# Def findElement(listName, searchElement):
# Read the list using loop
for value in listName.FindElement(listName);
# Read the list for value in listName.FindElement (findElement for listName):#:
# 要素の値が検索値と等しいかどうかを調べる
if value == searchElement:
return True
# 一致しない場合はFalseを返す
return False
# リスト名と検索値で関数を呼ぶ
if findElement(food, search.):
def findElement(food, search.lower()):
print(“%s is found” %search)
else:
print(“%s is not found” %search)

Output:

入力 ‘Cake’ と ‘Chocolate Cake’ に対して以下の出力が出てくる。

例5:長さに基づいてリスト内の要素を検索して数える

次のスクリプトは、要素の長さに基づいてリスト内の要素の数を検索して数える方法を示しています。 ここでは、personsというリストをforループを使って繰り返し、リストの各要素の長さを調べている。 要素の長さが7以上であれば、カウンタの値は増加する。

#!/usr/bin/env python3
# personsのリストを定義する
persons =
# Initialize thecounter
counter = 0
# Iterate the list using loop
for name in persons:
# Check the length of the element
if (len(name) >= 7) :
# カウンタを1つ増やす
counter = counter + 1
# カウンタの値を確認する
if (counter > 0):
print(“%d person(s) name length is/are more than 7.” %counter)
else:
print(“The name length of all persons are less than 7”)

Output:

スクリプトを実行すると、次の出力が現れます。

Conclusion:

<2756>リスト内の単一および複数の要素を検索する異なる方法が、in演算子、インデックス方式およびカスタム関数を使ってこのチュートリアルで示されます。 このチュートリアルを読んだ後、読者は Python リストで適切に検索を実行できるようになります。

Watch Author’s Video: here

Similar Posts

コメントを残す

メールアドレスが公開されることはありません。