# 如何获取元组列表中的第一个元素？

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

` `[1,2]` `

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

` `>>> inpt = [(1, u'abc'), (2, u'def')] >>> unzipped = zip(*inpt ) >>> print unzipped [(1, 2), (u'abc', u'def')] >>> print list(unzipped[0]) [1, 2]` `

` `print(list(zip(*inpt))[0]) (1, 2)` `

` `new_list = [ seq[0] for seq in yourlist ]` `

` `>>> my_list = [(1, u'abc'), (2, u'def')] >>> my_ids = [idx for idx, val in my_list] >>> my_ids [1, 2]` `

` `>>> x = (1, u'abc') >>> idx, val = x >>> idx 1 >>> val u'abc'` `

` `>>> a = [(1, u'abc'), (2, u'def')] >>> import operator >>> b = map(operator.itemgetter(0), a) >>> b [1, 2]` `

`itemgetter`语句返回一个函数 ，返回你指定的元素的索引。 写作完全一样

` `>>> b = map(lambda x: x[0], a)` `

` `>>> c = sorted(a, key=operator.itemgetter(0), reverse=True) >>> c [(2, u'def'), (1, u'abc')]` `

` `>>> a = [(1, u'abc'), (2, u'def')] >>> a [(1, u'abc'), (2, u'def')] >>> dict(a).keys() [1, 2] >>> dict(a).values() [u'abc', u'def'] >>>` `

` `l1 = [(1, u'abc'), (2, u'def')] l2 = [(tup[0],) for tup in l1] l2 >>> [(1,), (2,)]` `

` `>>> a = [(1, u'abc'), (2, u'def')] >>> import operator >>> b = map(operator.itemgetter(0), a) >>> b` `

` `[1, 2]` `

` `<map at 0xb387eb8>` `

` `>>> b = list(map(operator.itemgetter(0), a))` `