Python - 列表中唯一配對
Python 是一種非常常用的程式語言,全世界各地的程式設計師都將其用於各種不同的目的。Python 的應用領域包括 Web 開發、機器學習、資料科學以及執行許多不同的自動化流程。Python 將其資料儲存在不同的資料集中,例如列表、字典集等。每個程式設計師在處理列表時都必須經歷的一個類似過程是在列表中查詢唯一配對。在本文中,我們將學習可以用來查詢列表中唯一配對的不同方法。
在列表中查詢唯一配對的不同方法
巢狀迴圈
這是一種使用巢狀迴圈方法在列表中查詢唯一配對的非常簡單的方法。巢狀迴圈是指一個迴圈存在於另一個迴圈內部的情況。讓我們舉一個例子,以便更好地理解它。
示例
def checking_of_uniqueness(list): # The input of the list is given to a function checking_of_uniqueness
new_list = [] # A new list is created which will contain all the unique pairs
for i in range(len(list)): # In this loop each element in the list is checked
for j in range(i + 1, len(list)): # In this loop all remaining elements are checked after current index
unique_pair = (list[i], list[j]) # A tuple is created to store all the unique pairs in one variable (unique_pair)
new_list.append(unique_pair)
return new_list
# Example
names = ['Jack', 'Sam', 'John', 'Daniel'] # The input of list is given
final_pairs = checking_of_uniqueness(names) # The function checking_of_uniqueness is run
print(final_pairs) #The final output of different pairs is displayed
輸出
上面示例的輸出如下所示
[('Jack', 'Sam'), ('Jack', 'John'), ('Jack', 'Daniel'), ('Sam', 'John'), ('Sam', 'Daniel'), ('John', 'Daniel')]
Itertools
我們可以使用 itertools 的函式來查詢列表中存在的不同唯一配對。itertool 庫用於有效地檢查列表中存在的每個元素。讓我們舉一個例子,以便更好地理解這種方法。
示例
from itertools import combinations # Do not forget to import itertools or else error might occur
def checking_of_uniqueness(lists): # The input of the list is given to a function checking_of_uniqueness
return list(combinations(lists, 2)) #The combinations function of itertools is used to find unique pairs in the list
# Example
names = ['Jack', 'Sam', 'John', 'Daniel'] # The input of list is given
final_pairs = checking_of_uniqueness(names) # The function checking_of_uniqueness is run
print(final_pairs) #The final output of different pairs is displayed
輸出
上面示例的輸入如下所示
[('Jack', 'Sam'), ('Jack', 'John'), ('Jack', 'Daniel'), ('Sam', 'John'), ('Sam', 'Daniel'), ('John', 'Daniel')]
列表推導式
列表推導式用於生成建立新列表的更短語法。我們將使用列表推導式來建立一個包含唯一配對的新列表。讓我們舉一個例子,以便更好地理解它。
示例
def checking_of_uniqueness(list): # The input of the list is given to a function checking_of_uniqueness
return [(list[i], list[j]) for i in range(len(list)) for j in range(i + 1, len(list))] # We create two different nested loops in a single line of code with the help of list comprehension method to find the uniqueness using tuple
# Example
names = ['Jack', 'Sam', 'John', 'Daniel'] # The input of list is given
final_pairs = checking_of_uniqueness(names) # The function checking_of_uniqueness is run
print(final_pairs) #The final output of different pairs is displayed
輸出
上面示例的輸出如下所示
('Jack', 'Sam'), ('Jack', 'John'), ('Jack', 'Daniel'), ('Sam', 'John'), ('Sam', 'Daniel'), ('John', 'Daniel')]
集合
在這種方法中,我們將列表轉換為集合,然後刪除重複元素並在提供的資料中找到唯一配對。讓我們舉一個例子,以便更好地理解它。
示例
def checking_of_uniqueness(lst):# The input of the list is given to a function checking_of_uniqueness
unique_elements = set(lst) # We convert the list into sets which remove all the same elements in the list
unique_pairs = []
for i in unique_elements:
for j in unique_elements:
if i < j: # Ensure that the pairs are unique and ordered
unique_pairs.append((i, j))
return unique_pairs
# Example
names = ['Jack', 'Sam', 'John', 'Daniel'] # The input of list is given
final_pairs = checking_of_uniqueness(names) # The function checking_of_uniqueness is run
print(final_pairs) #The final output of different pairs is displayed
輸出
上面示例的輸出如下所示
[('Jack', 'Sam'), ('Jack', 'John'), ('Daniel', 'Jack'), ('Daniel', 'Sam'), ('Daniel', 'John'), ('John', 'Sam')])]
遞歸回溯
這是一種從列表中查詢唯一配對的複雜方法,但與上面建議的方法相比,這種方法非常有效。建議在列表包含大量資料的情況下使用此方法。它透過逐一檢查其中的所有不同元素來形成配對。讓我們舉一個例子,以便更好地理解它。
示例
def checking_of_uniqueness(lists):# The input of the list is given to a function checking_of_uniqueness def backtrack(start, path):
def backtrack(start, path):
if len(path) == 2: # Path is a temporary list to store the pairs as they are being built
unique_pairs.append(tuple(path)) # Once the pair is completed, it is moved into the unique_pairs
else:
for i in range(start, len(lists)): #If the pair remains incomplete the function calls itself and increments the function `start` and then updates itself according to it
if lists[i] not in path:
path.append(lists[i])
backtrack(i + 1, path)
path.pop() #This function removes the last element from the list
unique_pairs = []
backtrack(0, [])
return unique_pairs
# Example
names = ['Jack', 'Sam', 'John', 'Daniel'] # The input of list is given
final_pairs = checking_of_uniqueness(names) # The function checking_of_uniqueness is run
print(final_pairs) #The final output of different pairs is displayed
輸出
上面示例的輸出如下所示
[('Jack', 'Sam'), ('Jack', 'John'), ('Jack', 'Daniel'), ('Sam', 'John'), ('Sam', 'Daniel'), ('John', 'Daniel')]
結論
掌握查詢列表中唯一配對的不同方法對於成為一名高效的程式設計師非常重要。本文提供了可以用來從列表中查詢唯一配對的不同方法。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP