我有一系列列表,称它们为A
,B
,C
,D
,E
。现在,每个列表都有5个具有相同名称的元素,例如:
A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
etc..
我想要的是列表列表,形式为:
[ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'],
['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'],
['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'],
['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ]
可以看出,此列表的规则是:
['Cars_A', 'Planes_A']
是不允许的。['Cars_A', 'Cars_B']
。我现在的尝试是对所有5个列表进行嵌套循环,但我想尽可能避免这种情况。有任何想法吗?
答案 0 :(得分:2)
将itertools.permutations
和itertools.product
与filter
一起使用:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
答案 1 :(得分:0)
这是使用itertools.combinations
的一种简单方法,让我们先对所有配对进行配对,然后再对filter
进行配对。
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
答案 2 :(得分:0)
这是在没有itertools
的情况下执行此操作的一种方法,但是使用来自collections
模块的名为deque
的快速数据结构
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))