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')]
结论
掌握查找列表中唯一配对的不同方法对于成为一名高效的程序员非常重要。本文提供了可以用来从列表中查找唯一配对的不同方法。
广告