繁体   English   中英

python 3 中多个列表中两列的唯一组合/乘积

[英]Unique combination/product of two columns from multiple lists in python 3

如何从多个列表中获取每个可能的唯一值组合

输入:

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

预期 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') ........]

我知道这有很大的复杂性,而且根本不是最优的。 但至少这是产生所需的 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)

请帮助我以正确的方式编写它。

尝试来自 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)

这是我的解决方案。 output 但是与您的工作解决方案不同,其中包括两次('auroch', 'clover')等元素,因为put_in_unique_combination_list使用和or检查元组唯一性时。


config设置(list_n, {list_n + 1, ...]}的映射, chain用于将其他列表展平为单个可迭代。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM