从条件列表中选择值

时间:2022-01-14 18:15:04

I have a list of tuples. Every tuple has 5 elements (corresponding to 5 database columns) and I'd like to make a query

我有一个元组列表。每个元组都有5个元素(对应5个数据库列),我想进行查询

select attribute1 from mylist where attribute2 = something

从mylist中选择attribute1,其中attribute2 = something

e.g.

personAge = select age from mylist where person_id = 10

personAge =从mylist中选择年龄,其中person_id = 10

Is it possible to query the list of tuples in some way?

是否有可能以某种方式查询元组列表?

thank you

3 个解决方案

#1


50  

If you have named tuples you can do this:

如果您已命名元组,则可以执行以下操作:

results = [t.age for t in mylist if t.person_id == 10]

Otherwise use indexes:

否则使用索引:

results = [t[1] for t in mylist if t[0] == 10]

Or use tuple unpacking as per Nate's answer. Note that you don't have to give a meaningful name to every item you unpack. You can do (person_id, age, _, _, _, _) to unpack a six item tuple.

或者根据Nate的回答使用元组解包。请注意,您不必为解压缩的每个项目指定有意义的名称。您可以执行(person_id,age,_,_,_,_)来解包六项元组。

#2


12  

One solution to this would be a list comprehension, with pattern matching inside your tuple:

对此的一个解决方案是列表理解,在元组内部使用模式匹配:

>>> mylist = [(25,7),(26,9),(55,10)]
>>> [age for (age,person_id) in mylist if person_id == 10]
[55]

Another way would be using map and filter:

另一种方法是使用map和filter:

>>> map( lambda (age,_): age, filter( lambda (_,person_id): person_id == 10, mylist) )
[55]

#3


6  

Yes, you can use filter if you know at which position in the tuple the desired column resides. If the case is that the id is the first element of the tuple then you can filter the list like so:

是的,如果您知道所需列位于元组中的哪个位置,则可以使用过滤器。如果情况是id是元组的第一个元素,那么你可以像这样过滤列表:

filter(lambda t: t[0]==10, mylist)

This will return the list of corresponding tuples. If you want the age, just pick the element you want. Instead of filter you could also use list comprehension and pick the element in the first go. You could even unpack it right away (if there is only one result):

这将返回相应元组的列表。如果你想要年龄,只需选择你想要的元素。您也可以使用列表理解并在第一次选择元素而不是过滤器。您甚至可以立即解压缩(如果只有一个结果):

[age] = [t[1] for t in mylist if t[0]==10]

But I would strongly recommend to use dictionaries or named tuples for this purpose.

但我强烈建议为此目的使用词典或命名元组。

#1


50  

If you have named tuples you can do this:

如果您已命名元组,则可以执行以下操作:

results = [t.age for t in mylist if t.person_id == 10]

Otherwise use indexes:

否则使用索引:

results = [t[1] for t in mylist if t[0] == 10]

Or use tuple unpacking as per Nate's answer. Note that you don't have to give a meaningful name to every item you unpack. You can do (person_id, age, _, _, _, _) to unpack a six item tuple.

或者根据Nate的回答使用元组解包。请注意,您不必为解压缩的每个项目指定有意义的名称。您可以执行(person_id,age,_,_,_,_)来解包六项元组。

#2


12  

One solution to this would be a list comprehension, with pattern matching inside your tuple:

对此的一个解决方案是列表理解,在元组内部使用模式匹配:

>>> mylist = [(25,7),(26,9),(55,10)]
>>> [age for (age,person_id) in mylist if person_id == 10]
[55]

Another way would be using map and filter:

另一种方法是使用map和filter:

>>> map( lambda (age,_): age, filter( lambda (_,person_id): person_id == 10, mylist) )
[55]

#3


6  

Yes, you can use filter if you know at which position in the tuple the desired column resides. If the case is that the id is the first element of the tuple then you can filter the list like so:

是的,如果您知道所需列位于元组中的哪个位置,则可以使用过滤器。如果情况是id是元组的第一个元素,那么你可以像这样过滤列表:

filter(lambda t: t[0]==10, mylist)

This will return the list of corresponding tuples. If you want the age, just pick the element you want. Instead of filter you could also use list comprehension and pick the element in the first go. You could even unpack it right away (if there is only one result):

这将返回相应元组的列表。如果你想要年龄,只需选择你想要的元素。您也可以使用列表理解并在第一次选择元素而不是过滤器。您甚至可以立即解压缩(如果只有一个结果):

[age] = [t[1] for t in mylist if t[0]==10]

But I would strongly recommend to use dictionaries or named tuples for this purpose.

但我强烈建议为此目的使用词典或命名元组。