列表的笛卡尔积(无重复项)

5

给定一个数组 a=['a','b','c'],如何返回该数组的笛卡尔积且无重复元素。示例:

[['a', 'a' , 'a' ,'a']
['a', 'a' , 'a' ,'b']
['a', 'a' , 'a' ,'c']
['a', 'a' , 'b' ,'b']
['a', 'a' , 'b' ,'c']
['a', 'a' , 'c' ,'c']
...etc..]

在参考如何在Python中生成列表的所有排列后,我尝试了以下代码:

print list(itertools.permutations(['a', 'b' , 'c'], 4))
[]

print list(itertools.product(['a', 'b' , 'c'], repeat=4)

但是我会得到具有重复项的笛卡尔积。例如,列表将包含['a','a','b','b']['a','b','b','a']这两个明显相等的示例。

注意:我的'a','b','c'是存储数字1、2、3的变量。因此,在获取字母组合列表后,我需要执行以下操作:

['a','b','c','c'] ----> a*b*c*c = 1*2*3*3 = 18

在Python中,最快的方法是什么?使用NumPy进行操作是否可能/更快?

谢谢!
2个回答

11

也许你实际上想要combinations_with_replacement

>>> from itertools import combinations_with_replacement
>>> a = ['a', 'b', 'c']
>>> c = combinations_with_replacement(a, 4)
>>> for x in c:
...     print x
...     
('a', 'a', 'a', 'a')
('a', 'a', 'a', 'b')
('a', 'a', 'a', 'c')
('a', 'a', 'b', 'b')
('a', 'a', 'b', 'c')
('a', 'a', 'c', 'c')
('a', 'b', 'b', 'b')
('a', 'b', 'b', 'c')
('a', 'b', 'c', 'c')
('a', 'c', 'c', 'c')
('b', 'b', 'b', 'b')
('b', 'b', 'b', 'c')
('b', 'b', 'c', 'c')
('b', 'c', 'c', 'c')
('c', 'c', 'c', 'c')

如果没有更多关于如何将字符串映射到数字的信息,我无法评论您的第二个问题,但编写自己的product函数或使用numpy并不太困难。


-1

编辑: 不要使用这个; 请使用其他答案


If your original set is guaranteed uniqueness, then the `combinations_with_replacement` solution will work. If not, you can first pass it through `set()` to get it down to unique variables. Regarding the product, assuming you have the values stored in a dictionary `values` and that all the variables are valid python identifiers, you can do something like the following
combos = combinations_with_replacement(a, 4)
product_strings = ['*'.join(c) for c in combos]
products = [eval(s, globals(), values) for s in product_strings]

Needless to say, be very careful with eval. Only use this solution if you are creating the list a.

Example exploit: a = ['from os import', '; system("rm -rf .");']


我不明白 eval(s, globals(), values) 是做什么的? - Oniropolo
你传入的字符串类似于 "a*b*c*d"。当你使用 eval 时,第二个参数是要使用的本地字典。例如,{'a': 2, 'b': 3, 'c': 1, 'd': 1}。它告诉 Python 每个变量的值。 - Felipe
1
对于未来阅读此答案的任何人(因为它是最佳答案):每当您考虑使用eval时,请停下来。这通常是一个坏主意。请使用其他答案。 - AmphotericLewisAcid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接