作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个要压缩的列表
列表A:
["hello ", "world "]
列表B:
["one", "two", "three"]
我想像这样压缩列表中的元素:
[("hello","one")
("hello","two")
("hello","three")
("world","one")
("world","two")
("world","three")]
显然,我可以使用双 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')]
关于python - 将列表 A 的每个元素与列表 B 的每个元素压缩 - 最佳方法 "pythonie",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144919/
这个问题已经有答案了: How to get the Cartesian product of multiple lists (19 个回答) 已关闭 8 年前。 我有两个要压缩的列表 列表A: ["
我是一名优秀的程序员,十分优秀!