如何获取元组列表中的第一个元素? [英] How to get first element in a list of tuples?

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

问题描述

我有一个下面的列表,第一个元素是id,另一个是一个字符串:

I have a list like below where the first element is the id and the other is a string:

[(1, u'abc'), (2, u'def')]

我想创建一个只有从这个元组列表中的ids列表如下:

I want to create a list of ids only from this list of tuples as below:

[1,2]

我将在中的 __中使用此列表,因此需要一个整数列表值。请帮助!

I'll use this list in __in so it needs to be a list of integer values. Please help!

推荐答案

>>>a = [(1, u'abc'), (2, u'def')]
>>>b = [int(i[0]) for i in a]
[1, 2]

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

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