如何从不同列表中识别3个图层组合 [英] How to identify 3 layer combinations from different lists
问题描述
我有两个不同的图像数据集列表(列表1和列表2).考虑到两个列表的集成,我想确定可能的三种功能组合,但不能单独考虑.list1是主要数据集,list2是辅助数据.我已经熟悉 itertools.combinations
,但是我不知道如何解决当前问题.谁能在python中提出解决方案?
I have two different lists (list1 and list2) of image datasets. I want to determine possible three feature combinations considering the integration of both lists, but not individually. list1 is the main dataset and list2 is ancillary data. I am already familiar with itertools.combinations
but I don't know how to solve the current problem. Can anyone suggest a solution in python?
list1= ["a","b","c","d","e"]
list2= ["a1", "a2","a3","a4",
"a5","a6","a7","a8",
"a9","a10","a11","a12"]
五个可能组合的示例:
example for five possible combinations:
combinations=[('a', 'a1', 'a2'), ('a', 'b', 'a2'), ('b', 'a1', 'a2'), ('b', 'c', 'a3'), ('a', 'a2', 'a3')]
我测试了建议的解决方案的两个小列表,这会导致某些组合中成员的重复.
I tested the proposed solution for two small lists and this produce repetition of members in some combinations.
list1= ['a','b','c','d','e']
list2= ['a1', 'a2']
结果:
{('d', 'c', 'a2'), ('c', 'a2', 'a1'), ('e', 'b', 'a1'), ('c', 'd', 'a1'), ('e', 'a2', 'a1'), ('a', 'a', 'a2'), ('b', 'a2', 'a1'), ('d', 'd', 'a1'), ('d', 'a', 'a2'), ('a', 'b', 'a2'), ('a', 'e', 'a2'), ('e', 'a1', 'a1'), ('d', 'e', 'a2'), ('c', 'a', 'a2'), ('c', 'c', 'a2'), ('c', 'e', 'a2'), ('b', 'd', 'a1'), ('a', 'c', 'a1'), ('e', 'd', 'a1'), ('d', 'b', 'a2'), ('e', 'c', 'a2'), ('d', 'c', 'a1'), ('b', 'c', 'a2'), ('b', 'e', 'a2'), ('c', 'b', 'a2'), ('a', 'a', 'a1'), ('a', 'a1', 'a2'), ('d', 'a', 'a1'), ('a', 'e', 'a1'), ('a', 'b', 'a1'), ('b', 'a', 'a2'), ('d', 'a1', 'a2'), ('d', 'e', 'a1'), ('e', 'e', 'a2'), ('a', 'a2', 'a2'), ('c', 'a', 'a1'), ('c', 'c', 'a1'), ('a', 'd', 'a2'), ('d', 'a2', 'a2'), ('c', 'a1', 'a2'), ('c', 'e', 'a1'), ('b', 'a1', 'a1'), ('d', 'b', 'a1'), ('e', 'a', 'a2'), ('e', 'c', 'a1'), ('b', 'b', 'a2'), ('b', 'a1', 'a2'), ('b', 'c', 'a1'), ('e', 'b', 'a2'), ('c', 'a2', 'a2'), ('c', 'd', 'a2'), ('b', 'e', 'a1'), ('c', 'b', 'a1'), ('e', 'a2', 'a2'), ('a', 'a1', 'a1'), ('d', 'd', 'a2'), ('b', 'a2', 'a2'), ('b', 'a', 'a1'), ('e', 'a1', 'a2'), ('d', 'a1', 'a1'), ('e', 'e', 'a1'), ('a', 'a2', 'a1'), ('a', 'd', 'a1'), ('e', 'd', 'a2'), ('d', 'a2', 'a1'), ('c', 'a1', 'a1'), ('b', 'd', 'a2'), ('a', 'c', 'a2'), ('e', 'a', 'a1'), ('b', 'b', 'a1')}
重复的例子:('c','a1','a1')
推荐答案
收集所有所需信息后,我建议的解决方案是使用 product
:
After gathering all the information required, my proposed solution is to use a product
instead:
from itertools import product
def apply_op(list1, list2):
part_1 = product(list1, list1, list2)
part_2 = product(list1, list2, list2)
return set(list(part_1)).union(set(list(part_2)))
print(apply_op(A, B))
以上解决方案不起作用,因为结果在元组中包含重复的成员.
above solution doesn't work, since the result contains duplicate members in tuples.
def apply_op(list1, list2):
ret = []
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
for k in range(len(list2)):
ret.append((list1[i], list1[j], list2[k]))
for i in range(len(list1)):
for j in range(len(list2)):
for k in range(j + 1, len(list2)):
ret.append((list1[i], list2[j], list2[k]))
return ret
print(apply_op(A, B))
这篇关于如何从不同列表中识别3个图层组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!