我有两个要压缩的列表
清单 A:
["hello ", "world "]
Run Code Online (Sandbox Code Playgroud)
名单乙:
["one", "two", "three"]
Run Code Online (Sandbox Code Playgroud)
我想像这样压缩列表中的元素:
[("hello","one")
("hello","two")
("hello","three")
("world","one")
("world","two")
("world","three")]
Run Code Online (Sandbox Code Playgroud)
显然,我可以使用 double for 循环并附加元素,但我想知道这样做的好的 pythonie 方法是什么?
这似乎是一个完美的用例 itertools.product
>>> import itertools
>>> list(itertools.product(['hello', 'world'], ['one', 'two', 'three']))
[('hello', 'one'), ('hello', 'two'), ('hello', 'three'), ('world', 'one'), ('world', 'two'), ('world', 'three')]
Run Code Online (Sandbox Code Playgroud)