Python 列表中的唯一对

Python 列表中的唯一对

Python是一种广泛使用的编程语言,被全世界的程序员用于不同的目的。Python的应用领域包括网站开发、机器学习、数据科学和自动化等多种不同的处理过程。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库用于有效地检查列表中的每个元素。让我们举一个例子来更好地理解这种方法:

示例

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')]

结论

了解不同的方法来查找列表中的唯一对是成为高效程序员的重要知识。本文介绍了可以用来从列表中查找唯一对的不同方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程