Python 找到一个元组列表中的元组索引
为了找到一个元组列表中的元组索引,我们可以使用Python的enumerate函数和index函数。
有序且不可变的对象组被称为元组。元组和列表都是序列。区别在于元组是不可更改的,而列表是可更改的,并且元组使用圆括号而列表使用方括号。
示例
假设我们有一个包含元组的输入列表和另一个搜索元组列表。现在,我们将在给定的输入列表中搜索搜索元组列表的索引。
输入
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
输出
Index of searched tuple in the input list: [0, 2]
使用列表推导、查找字典和enumerate()函数
enumerate()方法会将可迭代对象中的元素添加计数器,并返回enumerate对象。
语法
enumerate(iterable, start=0)
参数
- iterable - 它可以是任何支持迭代的序列/对象/可迭代对象。
-
start - enumerate() 从该值开始计数。如果未指定 start 值,则使用 0。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入的元组列表 。
-
打印 输入的列表 。
-
创建另一个变量来存储输入的搜索元组列表,其值将在输入的列表中进行搜索。
-
使用 enumerate() 函数获取输入列表的元素和索引值作为值-键对。
-
使用列表推导来遍历 searchTuple 列表,并检查元素是否存在于上述查找字典中。
-
如果存在,则将其索引存储起来。
示例
以下程序使用列表推导、查找字典和 enumerate() 函数从其他元组列表返回元组索引:
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
# printing input list
print("Input List: ", inputList)
# input search tuple list whose values are to be searched in input list
searchTupleList = [(10, 6), (5, 7), (3, 8), (7, 2)]
# getting the element and index values of input list as a value-key pairs
searchDict = {v: k for k, v in enumerate(inputList)}
# Checking whether the tuple exits and if exists then storing the index of it.
resultantList = [searchDict[idx]
for idx in searchTupleList if idx in searchDict]
# printing resultant list of index
print("Index of searched tuple in input list:", resultantList)
输出
执行上述程序后,将生成以下输出 –
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)]
Index of searched tuple in input list: [2]
使用列表推导和enumerate()函数
当您希望根据现有列表的值来构建新列表时,列表推导提供了更简洁的语法。
示例
以下程序使用列表推导和enumerate()函数从其他元组列表中返回元组索引-
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
# printing input list
print("Input List: ", inputList)
# input search tuple list whose values are to be searched in input list
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
# Getting the result using the concise syntax using list comprehension
resultantList = [i for i, value in enumerate(inputList) for element in searchTupleList if element == value]
# printing resultant list of indices
print("Index of searched tuple in input list:", resultantList)
输出
在执行时,上述程序将生成以下输出 –
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)]
Index of searched tuple in input list: [0, 2]
使用index()方法
index()方法返回提供的值第一次出现的位置。
语法
list.index(element)
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入的元组列表 。
-
打印输入列表。
-
创建另一个变量来存储要在输入列表中搜索的输入搜索元组列表。
-
初始化一个新的空列表来存储结果列表。
-
使用 for循环 遍历searchTupleList。
-
使用 if条件 语句检查当前元素是否存在于输入列表中。
-
使用 index() 函数获取当前元素的索引,并使用 append() 函数将其添加到结果列表中(在列表末尾添加元素)。
-
打印结果列表。
示例
以下程序通过使用列表推导和enumerate()函数从另一个元组列表返回元组索引:
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
# printing input list
print("Input List: ", inputList)
# input search tuple list whose values are to be searched in the input list
searchTupleList = [(10, 6), (5, 7), (3, 8), (7, 2)]
# Creating a variable to store the result indices list
resultantList=[]
# traversing through searchTupleList
for p in searchTupleList:
# checking whether the current element is present in an input list
if p in inputList:
# getting the index of current element and appending it to the resultant list
resultantList.append(inputList.index(p))
# printing resultant list of indices
print("Index of searched tuple in input list:", resultantList)
输出
执行上述程序后,将生成以下输出结果:
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)]
Index of searched tuple in input list: [2]
结论
在这篇文章中,我们学习了如何使用3种不同的方法从其他元组列表中找到元组索引。我们还学会了如何使用enumerate()函数遍历可迭代对象的索引和元素值。最后,我们学会了如何使用index()函数来检索元组对。