python - 从两个列表中获取对应的元素

标签 python list

我有两个列表 test1 和 test2。如何将前两个索引拆分为单独的列表

 test1 = [a,1,x]
 test2 = [b,2,y]

最终列表应该是这样的。

 first_alpha = [a,b]
 number = [1,2]
 last_alpha = [x,y]

最佳答案

压缩列表,然后将它们解压到目标变量中。

first_alpha, number, last_alpha = zip(test1, test2)
>>> first_alpha, number, last_alpha
(('a', 'b'), (1, 2), ('x', 'y'))

关于python - 从两个列表中获取对应的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58907759/

相关文章:

Python2.6 xmpp Jabber错误

Python:检查对象是否不是 "array-type"

java - 进一步排序一个已经排序的列表

Python 的 SQLAlchemy 不会清除辅助(多对多)表?

python - 如何为 matplotlib : Font family ['cursive' ] not found 安装草书字体

Python 脚本分发

python - 我们什么时候应该在 Python 项目中调用 logging.basicConfig?

c - 尝试将节点插入链表时出现段错误

css - 将无序列表置于 DIV 中心

c# - 在 List<Product> 中搜索产品名称