如何从Python字典列表中获取值?

如何从Python字典列表中获取值?,python,list,python-2.7,dictionary,Python,List,Python 2.7,Dictionary,我有一本字典的目录。词典的结构是 stop_id : "" stop_name : "" 现在,我有一个名称字符串,它将与stop\u name匹配,并获得与之对应的stop\u id 有什么有效的方法吗?我只能想出一个循环 for entry in stop_list: if name = entry['name']: id = entry['id'] break 您可以使用生成器表达式和next函数,

我有一本字典的目录。词典的结构是

stop_id : ""
stop_name : ""
现在,我有一个名称字符串,它将与
stop\u name
匹配,并获得与之对应的
stop\u id

有什么有效的方法吗?我只能想出一个循环

for entry in stop_list:
            if name = entry['name']:
                id = entry['id']
                break

您可以使用生成器表达式和
next
函数,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}
这将遍历
停止列表
,当找到匹配项时,将生成
条目['id']
。这会更好,因为它不必迭代整个列表

另一个优点是,如果有多个匹配项,那么也可以使用相同的表达式来获取下一个id,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}

如果要进行多个查找,并且给定名称是唯一的,则对列表进行预处理并创建字典,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}

然后您可以使用
lookup[name]
以固定时间进行查找。如果名称是唯一的,并且有多个查找,这将是最有效的方法,您可以使用生成器表达式和下一个函数,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}
这将遍历
停止列表
,当找到匹配项时,将生成
条目['id']
。这会更好,因为它不必迭代整个列表

另一个优点是,如果有多个匹配项,那么也可以使用相同的表达式来获取下一个id,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}

如果要进行多个查找,并且给定名称是唯一的,则对列表进行预处理并创建字典,如下所示

next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> ids = next(entry['id'] for entry in stop_list if entry['name'] == name)
>>> next(ids)
# you will get the first matching id here
>>> next(ids)
# you will get the second matching id here
lookup = {entry['name']: entry['id'] for entry in stop_list}

然后您可以使用
lookup[name]
以固定时间进行查找。如果名称是唯一的,并且如果有多个查找,那么这将是最有效的方法。在查看代码后,您会发现字典具有相同的名称。 如果你的名字是唯一的,你应该考虑为你的DICT使用一个DICT,关键字是名称。 1-这将允许您不浏览列表(与dict查找相比成本较高)

2-这更具可读性

3-您只调用条目['name']一次

假设你的stopdict看起来是这样的

stopdict= {
    'stop1' : {'name' : 'stop1', 'id' : 1}
    'stop2' : {'name' : 'stop2', 'id' : 2}
}
访问id的方式如下所示:

stopdict[entry['name']]['id']

在查看代码之后,您似乎得到了同名词典。 如果你的名字是唯一的,你应该考虑为你的DICT使用一个DICT,关键字是名称。 1-这将允许您不浏览列表(与dict查找相比成本较高)

2-这更具可读性

3-您只调用条目['name']一次

假设你的stopdict看起来是这样的

stopdict= {
    'stop1' : {'name' : 'stop1', 'id' : 1}
    'stop2' : {'name' : 'stop2', 'id' : 2}
}
访问id的方式如下所示:

stopdict[entry['name']]['id']

为什么不创建映射
{stop\u name:stop\u id,…}
(如果名称是唯一的)或
{stop\u name:[stop\u id,…],…}
(如果不是)?您构建它一次,然后按名称查找是
O(1)
。为什么不创建映射
{stop\u name:stop\u id,…}
(如果名称是唯一的)或
{stop\u name:[stop\u id,…],…}
(如果不是)?您构建它一次,然后按名称查找是
O(1)
。我确信,不会有第二个匹配项。那么,这会奏效吗?或者用另一种更好的方式来考虑不会有第二项。@IshanBhatt您会不止一次地查找吗?不,只有一次查找。@IshanBhatt那么您在问题本身中的内容就足够了。我确信,不会有第二个匹配项。那么,这会奏效吗?或者用另一种更好的方式来考虑不会有第二项。@IshanBhatt您会不止一次地查找吗?不,只有一次查找。@IshanBhatt那么您在问题本身中所拥有的就足够了。