arr1=['One','Two','Five'],arr2=['Three','Four']
像itertools.combinations(arr1,2)
给我们('OneTwo','TwoFive','OneFive')
我想知道是否有任何方法可以将其应用于两个不同的数组。?我的意思是 arr1 和 arr2。
输出应为 OneThree、OneFour、TwoThree、TwoFour、FiveThree、FiveFour
最佳答案
您正在寻找.product()
:
从文档中,它执行以下操作:
product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
示例代码:
>>> x = itertools.product(arr1, arr2)
>>> for i in x: print i
('One', 'Three')
('One', 'Four')
('Two', 'Three')
('Two', 'Four')
('Five', 'Three')
('Five', 'Four')
将它们组合起来:
# This is the full code
import itertools
arr1 = ['One','Two','Five']
arr2 = ['Three','Four']
combined = ["".join(x) for x in itertools.product(arr1, arr2)]
关于python - python中两个数组(不同大小)之间可能的唯一组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38538786/