我有一个列表列表,需要提取每个项目;最终写出他们的笛卡尔积。
from itertools import product
b = {}
a = [[1, 2], [3, 4], ['a', 'b']]
for i, v in enumerate(a):
b[i] = v
#These don't work because we are feeding a single list to product
print list(product(x for x in a)) # [([1, 2],), ([3, 4],), (['a', 'b'],)]
print list(product(b.values())) # [([1, 2],), ([3, 4],), (['a', 'b'],)]
product
如果我能够正确使用,则print list(product([1, 2], [3, 4], ['a', 'b']))
[(1, 3, 'a'), (1, 3, 'b'), (1, 4, 'a'), (1, 4, 'b'), (2, 3, 'a'), (2, 3, 'b'), (2, 4, 'a'), (2, 4, 'b')]
正常工作:
Table table = new DynamoDB(new AmazonDynamoDBClient()).getTable("TableName");
答案 0 :(得分:1)
使用a
将itertools.product
解包到*
。示例 -
print list(product(*a))
解包会将a
的每个元素作为单独的参数发送到product
,而不是单个列表(就像 - list(product([1, 2], [3, 4], ['a', 'b']))
)。
演示 -
>>> a = [[1, 2], [3, 4], ['a', 'b']]
>>> from itertools import product
>>> print(list(product(*a)))
[(1, 3, 'a'), (1, 3, 'b'), (1, 4, 'a'), (1, 4, 'b'), (2, 3, 'a'), (2, 3, 'b'), (2, 4, 'a'), (2, 4, 'b')]