如何将元组列表转换为多个列表? [英] How to convert list of tuples to multiple lists?

查看:90
本文介绍了如何将元组列表转换为多个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个元组列表,我想转换为多个列表.

Suppose I have a list of tuples and I want to convert to multiple lists.

例如,元组列表是

[(1,2),(3,4),(5,6),]

Python中是否有任何内置函数可以将其转换为:

Is there any built-in function in Python that convert it to:

[1,3,5],[2,4,6]

这可以是一个简单的程序.但是我只是对Python中存在这种内置函数感到好奇.

This can be a simple program. But I am just curious about the existence of such built-in function in Python.

推荐答案

内置函数zip()几乎可以满足您的要求:

The built-in function zip() will almost do what you want:

>>> zip(*[(1, 2), (3, 4), (5, 6)])
[(1, 3, 5), (2, 4, 6)]

唯一的区别是您得到的是元组而不是列表.您可以使用

The only difference is that you get tuples instead of lists. You can convert them to lists using

map(list, zip(*[(1, 2), (3, 4), (5, 6)]))

这篇关于如何将元组列表转换为多个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆