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()函式檢索元組對。