[英]Unique combination/product of two columns from multiple lists in python 3
How can I get every possible unique combination of values from multiple lists?如何从多个列表中获取每个可能的唯一值组合?
Input:输入:
list_1 = ['copper', 'auroch', 'mirror']
list_2 = ['garland', 'clover', 'cocoahollow']
list_3 = ['garland', 'seraph', 'yolkwing']
list_4 = ['garland', 'clover', 'cocoahollow']
list_5 = ['riftwater', 'mothersday', 'snowsquall']
Expected Output:预期 Output:
unique_combination = [('copper', 'garland'), ('copper', 'clover'), ('copper', 'cocoahollow'), ('copper', 'seraph'), ('copper', 'yolkwing'), ('copper', 'riftwater'), ('copper', 'mothersday'), ('copper', 'snowsquall'), ('garland', 'auroch'), ('garland', 'mirror'), ('garland', 'garland'), ('garland', 'seraph'), ('garland', 'yolkwing'),('garland', 'clover'), ('garland', 'cocoahollow') ........]
I know this has huge complexity and it's not at all optimal.我知道这有很大的复杂性,而且根本不是最优的。 But at least this is producing the desired output.但至少这是产生所需的 output。
list_1 = ['copper', 'auroch', 'mirror']
list_2 = ['garland', 'clover', 'cocoahollow']
list_3 = ['garland', 'seraph', 'yolkwing']
list_4 = ['garland', 'clover', 'cocoahollow']
list_5 = ['riftwater', 'mothersday', 'snowsquall']
unique_combination = []
def put_in_unique_combination_list(input_parent_1, input_parent_2, unique_combination):
if (input_parent_1, input_parent_2) not in unique_combination or (
input_parent_2, input_parent_1) not in unique_combination:
unique_combination.append((input_parent_1, input_parent_2))
return unique_combination
for each_parent1_list, each_parent2_lists in [
(list_1, [list_2, list_3, list_4, list_5]),
(list_2, [list_3, list_4, list_5]),
(list_3, [list_4, list_5]),
(list_4, [list_5])]:
for each_item_in_parent1_list in each_parent1_list:
parent_1 = each_item_in_parent1_list
for each_parent2_list in each_parent2_lists:
for each_item_in_parent2_list in each_parent2_list:
parent_2 = each_item_in_parent2_list
unique_combination = put_in_unique_combination_list(parent_1, parent_2, unique_combination)
print(unique_combination)
Please help me to write it in the proper way.请帮助我以正确的方式编写它。
Try product from itertools and convert tuples to set(for uniqueness) and then convert back to tuple:尝试来自 itertools 的产品并将元组转换为集合(为了唯一性),然后转换回元组:
from itertools import product
list_1 = ['copper', 'auroch', 'mirror']
list_2 = ['garland', 'clover', 'cocoahollow']
list_3 = ['garland', 'seraph', 'yolkwing']
list_4 = ['garland', 'clover', 'cocoahollow']
list_5 = ['riftwater', 'mothersday', 'snowsquall']
unique_combination = list(product(list_1, list_2, list_3, list_4, list_5))
unique_combination = list(map(lambda x: set(x), unique_combination))
unique_combination = list(map(lambda x: tuple(x), unique_combination))
print(unique_combination)
Here is my solution.这是我的解决方案。 The output however differs to your working solution which includes elements like ('auroch', 'clover')
twice due to put_in_unique_combination_list
using and or
when checking for tuple uniqueness. output 但是与您的工作解决方案不同,其中包括两次('auroch', 'clover')
等元素,因为put_in_unique_combination_list
使用和or
检查元组唯一性时。
config
sets up the mapping of (list_n, {list_n + 1, ...]}
and chain
is used to flatten the other lists into a single iterable. config
设置(list_n, {list_n + 1, ...]}
的映射, chain
用于将其他列表展平为单个可迭代。
frozenset
just makes each item in the group unique. frozenset
只是使组中的每个项目都是唯一的。
from itertools import chain, product
all_lists = [
['copper', 'auroch', 'mirror'],
['garland', 'clover', 'cocoahollow'],
['garland', 'seraph', 'yolkwing'],
['garland', 'clover', 'cocoahollow'],
['riftwater', 'mothersday', 'snowsquall']
]
unique_combination = set()
config = [
(all_lists[i], frozenset(chain.from_iterable(all_lists[i+1:])))
for i in range(len(all_lists) - 1)
]
for lst, others in config:
for pair in product(lst, others):
# Remove this is (x,y) and (y,x) is allowed
if pair not in unique_combination and (pair[1], pair[0]) not in unique_combination:
unique_combination.add(pair)
# Output
from pprint import pprint
pprint(sorted(unique_combination))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.