python - 获取元组列表第一个索引? [英] python - get list of tuples first index?

查看:18
本文介绍了python - 获取元组列表第一个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

返回以下内容的最简洁方式是什么:

What's the most compact way to return the following:

给定一个元组列表,返回一个由元组第一个(或第二个,无关紧要)元素组成的列表.

Given a list of tuples, return a list consisting of the tuples first (or second, doesn't matter) elements.

为:

[(1,'one'),(2,'two'),(3,'three')]

返回列表将是

[1,2,3]

推荐答案

如果两者都需要,请使用 zip

use zip if you need both

>>> r=(1,'one'),(2,'two'),(3,'three')
>>> zip(*r)
[(1, 2, 3), ('one', 'two', 'three')]

这篇关于python - 获取元组列表第一个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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