从列表中获取所有成对组合的Python程序
给出一个列表。任务是编写一个Python程序来从列表中获取所有成对组合。
查找所有对(无唯一性)
例子:
Input: [1,”Mallika”,2,”Yash”]
Output: [(1, ‘Mallika’), (1, 2), (1, ‘Yash’), (‘Mallika’, 1), (‘Mallika’, 2), (‘Mallika’, ‘Yash’), (2, 1), (2, ‘Mallika’), (2, ‘Yash’), (‘Yash’, 1), (‘Yash’, ‘Mallika’), (‘Yash’, 2)]
方法 1:使用简单循环
我们可以使用两个循环来遍历列表索引来访问列表的所有组合。如果两个索引计数器都在相同的索引值上,我们跳过它,否则我们按顺序打印索引 i 处的元素,然后是索引 j 处的元素。
该方法的时间复杂度为 O(n 2 ),因为我们需要两个循环来迭代列表。
Python3
# declaring a list
lst = [1,"Mallika",2,"Yash"]
# output
output = []
# looping over list
for i in range(0,len(lst)):
for j in range(0,len(lst)):
# checking if i and j indexes are not on same element
if (i!=j):
# add to output
output.append((lst[i],lst[j]))
# view output
print(output)
Python3
# importing required library
import itertools
# creating a list of elements belonging to integers and strings
lst = [1, "Mallika", 2, "Yash"]
# simulating permutations of the list in a group of 2
pair_order_list = itertools.permutations(lst, 2)
# printing the elements belonging to permutations
print(list(pair_order_list))
Python3
# importing required ibrary
import itertools
# declaring a list
lst = [2,2,2]
# creating a list of pairs of the list
ordered_list = itertools.permutations(lst,2)
# looping over the elements belonging to the
# pair of permutations
for i in ordered_list:
# printing ith element
print(i)
Python3
# importing required library
import itertools
# creating a list of elements belonging
3 to integers and strings
lst = [1,"Mallika",2,"Yash"]
# simulating permutations of the list in
# a group of 2
pair_order_list = itertools.combinations(lst,2)
# printing the elements belonging to permutations
print (list(pair_order_list))
输出:
[(1, ‘Mallika’), (1, 2), (1, ‘Yash’), (‘Mallika’, 1), (‘Mallika’, 2), (‘Mallika’, ‘Yash’), (2, 1), (2, ‘Mallika’), (2, ‘Yash’), (‘Yash’, 1), (‘Yash’, ‘Mallika’), (‘Yash’, 2)]
方法二:使用itertools
Python提供对itertools标准库的支持,该库用于创建迭代器以实现高效循环。该库为各种迭代、分组、排序等提供支持。该库的 permutations() 函数用于遍历元素列表的所有可能排序,没有任何重复。 permutations() 函数具有以下语法:
itertools.permutations(lst,r)
其中 r 表示长度为 r 的元组,即 2 表示一对,3 表示三元组。第一个参数是指定的列表。
该函数返回形成排列后返回的元素组列表。输出包含 nx (n-1) 个元素,其中 n 是列表的大小,因为每个元素随后都会与所有其他元素相乘。计算排列所需的时间大致按列表大小的顺序呈指数增长。
蟒蛇3
# importing required library
import itertools
# creating a list of elements belonging to integers and strings
lst = [1, "Mallika", 2, "Yash"]
# simulating permutations of the list in a group of 2
pair_order_list = itertools.permutations(lst, 2)
# printing the elements belonging to permutations
print(list(pair_order_list))
输出:
[(1, ‘Mallika’), (1, 2), (1, ‘Yash’), (‘Mallika’, 1), (‘Mallika’, 2), (‘Mallika’, ‘Yash’), (2, 1), (2, ‘Mallika’), (2, ‘Yash’), (‘Yash’, 1), (‘Yash’, ‘Mallika’), (‘Yash’, 2)]
笔记:
- 这些对按照列表中元素到达的顺序打印。
- 在所有相同元素的情况下,该方法仍然继续形成对并返回它们,即使它们是重复的。
蟒蛇3
# importing required ibrary
import itertools
# declaring a list
lst = [2,2,2]
# creating a list of pairs of the list
ordered_list = itertools.permutations(lst,2)
# looping over the elements belonging to the
# pair of permutations
for i in ordered_list:
# printing ith element
print(i)
输出:
(2, 2)
(2, 2)
(2, 2)
(2, 2)
(2, 2)
(2, 2)
查找所有唯一对(唯一性)
但是,排列的方法不区分 (a, b) 和 (b, a) 对并返回它们。 itertools 库还支持组合 () 方法,该方法打印 (a, b) 或 (b, a) 对之一,而不是两者。输出的元素个数等价于(n-1)!其中 n 是列表的长度。计算组合所需的时间大致为多项式。
例子:
Input: [1,”Mallika”,2,”Yash”]
Output: [(1, ‘Mallika’), (1, 2), (1, ‘Yash’), (‘Mallika’, 2), (‘Mallika’, ‘Yash’), (2, ‘Yash’)]
蟒蛇3
# importing required library
import itertools
# creating a list of elements belonging
3 to integers and strings
lst = [1,"Mallika",2,"Yash"]
# simulating permutations of the list in
# a group of 2
pair_order_list = itertools.combinations(lst,2)
# printing the elements belonging to permutations
print (list(pair_order_list))
输出:
[(1, ‘Mallika’), (1, 2), (1, ‘Yash’), (‘Mallika’, 2), (‘Mallika’, ‘Yash’), (2, ‘Yash’)]