我有两个要压缩的列表
列表A:
["hello ", "world "]
列表B:
["one", "two", "three"]
我想像这样压缩列表中的元素:
[("hello","one")
("hello","two")
("hello","three")
("world","one")
("world","two")
("world","three")]
显然,我可以使用双循环并附加元素,但我想知道这样做的好方法是什么?
答案 0 :(得分:3)
这似乎是itertools.product
>>> import itertools
>>> list(itertools.product(['hello', 'world'], ['one', 'two', 'three']))
[('hello', 'one'), ('hello', 'two'), ('hello', 'three'), ('world', 'one'), ('world', 'two'), ('world', 'three')]