Zip every element of list A with every element of list B - best “pythonie” way of doing this
本问题已经有最佳答案,请猛点这里访问。
我有两个要压缩的列表
列表A:
1 | ["hello","world"] |
列表B:
1 | ["one","two","three"] |
号
我想压缩列表中的元素,如下所示:
1 2 3 4 5 6 | [("hello","one") ("hello","two") ("hello","three") ("world","one") ("world","two") ("world","three")] |
显然,我可以使用double for loop并附加元素,但是我想知道什么是一种好的pythonie方法?
这似乎是
1 2 3 | >>> import itertools >>> list(itertools.product(['hello', 'world'], ['one', 'two', 'three'])) [('hello', 'one'), ('hello', 'two'), ('hello', 'three'), ('world', 'one'), ('world', 'two'), ('world', 'three')] |