在Python中的列表中获取每个元组的第一个元素

时间:2021-10-13 13:31:53

An SQL query gives me a list of tuples, like this:

SQL查询为我提供了一个元组列表,如下所示:

[(elt1, elt2), (elt1, elt2), (elt1, elt2), (elt1, elt2), (elt1, elt2), ...]

I'd like to have all the first elements of each tuple. Right now I use this:

我想拥有每个元组的所有第一个元素。现在我用这个:

rows = cur.fetchall()
res_list = []
for row in rows:
    res_list += [row[0]]

But I think there might be a better syntax to do it. Do you know a better way?

但我认为可能有更好的语法来做到这一点。你知道更好的方法吗?

5 个解决方案

#1


110  

Use a list comprehension:

使用列表理解:

res_list = [x[0] for x in rows]

Below is a demonstration:

以下是演示:

>>> rows = [(1, 2), (3, 4), (5, 6)]
>>> [x[0] for x in rows]
[1, 3, 5]
>>>

Alternately, you could use unpacking instead of x[0]:

或者,您可以使用解包而不是x [0]:

res_list = [x for x,_ in rows]

Below is a demonstration:

以下是演示:

>>> lst = [(1, 2), (3, 4), (5, 6)]
>>> [x for x,_ in lst]
[1, 3, 5]
>>>

Both methods practically do the same thing, so you can choose whichever you like.

这两种方法实际上都做同样的事情,所以你可以选择你喜欢的任何一种。

#2


16  

If you don't want to use list comprehension by some reasons, you can use map and operator.itemgetter:

如果由于某些原因不想使用列表推导,可以使用map和operator.itemgetter:

>>> from operator import itemgetter
>>> rows = [(1, 2), (3, 4), (5, 6)]
>>> map(itemgetter(1), rows)
[2, 4, 6]
>>>

#3


10  

You can use list comprehension:

您可以使用列表理解:

res_list = [i[0] for i in rows]

This should make the trick

这应该成功

#4


8  

The functional way of achieving this is to unzip the list using:

实现此目的的功能方法是使用以下命令解压缩列表:

sample = [(2, 9), (2, 9), (8, 9), (10, 9), (23, 26), (1, 9), (43, 44)]
first,snd = zip(*sample)
print first,snd
(2, 2, 8, 10, 23, 1, 43) (9, 9, 9, 9, 26, 9, 44)

#5


5  

res_list = [x[0] for x in rows]

c.f. http://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

C.F. http://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

For a discussion on why to prefer comprehensions over higher-order functions such as map, go to http://www.artima.com/weblogs/viewpost.jsp?thread=98196.

有关为何更喜欢对高阶函数(例如map)的理解的讨论,请访问http://www.artima.com/weblogs/viewpost.jsp?thread=98196。

#1


110  

Use a list comprehension:

使用列表理解:

res_list = [x[0] for x in rows]

Below is a demonstration:

以下是演示:

>>> rows = [(1, 2), (3, 4), (5, 6)]
>>> [x[0] for x in rows]
[1, 3, 5]
>>>

Alternately, you could use unpacking instead of x[0]:

或者,您可以使用解包而不是x [0]:

res_list = [x for x,_ in rows]

Below is a demonstration:

以下是演示:

>>> lst = [(1, 2), (3, 4), (5, 6)]
>>> [x for x,_ in lst]
[1, 3, 5]
>>>

Both methods practically do the same thing, so you can choose whichever you like.

这两种方法实际上都做同样的事情,所以你可以选择你喜欢的任何一种。

#2


16  

If you don't want to use list comprehension by some reasons, you can use map and operator.itemgetter:

如果由于某些原因不想使用列表推导,可以使用map和operator.itemgetter:

>>> from operator import itemgetter
>>> rows = [(1, 2), (3, 4), (5, 6)]
>>> map(itemgetter(1), rows)
[2, 4, 6]
>>>

#3


10  

You can use list comprehension:

您可以使用列表理解:

res_list = [i[0] for i in rows]

This should make the trick

这应该成功

#4


8  

The functional way of achieving this is to unzip the list using:

实现此目的的功能方法是使用以下命令解压缩列表:

sample = [(2, 9), (2, 9), (8, 9), (10, 9), (23, 26), (1, 9), (43, 44)]
first,snd = zip(*sample)
print first,snd
(2, 2, 8, 10, 23, 1, 43) (9, 9, 9, 9, 26, 9, 44)

#5


5  

res_list = [x[0] for x in rows]

c.f. http://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

C.F. http://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

For a discussion on why to prefer comprehensions over higher-order functions such as map, go to http://www.artima.com/weblogs/viewpost.jsp?thread=98196.

有关为何更喜欢对高阶函数(例如map)的理解的讨论,请访问http://www.artima.com/weblogs/viewpost.jsp?thread=98196。