在 Python 中搜索字典列表的最简单方法是什么?

发布于 2024-09-26 23:14:03 字数 426 浏览 5 评论 0原文

我的数据库当前返回一个字典列表:

id_list = ({'id': '0c871320cf5111df87da000c29196d3d'}, 
           {'id': '2eeeb9f4cf5111df87da000c29196d3d'}, 
           {'id': '3b982384cf5111df87da000c29196d3d'}, 
           {'id': '3f6f3fcecf5111df87da000c29196d3d'}, 
           {'id': '44762370cf5111df87da000c29196d3d'}, 
           {'id': '4ba0d294cf5111df87da000c29196d3d'})

如何轻松检查给定的 id 是否在此列表中?

谢谢。

My database currently returns a list of dicts:

id_list = ({'id': '0c871320cf5111df87da000c29196d3d'}, 
           {'id': '2eeeb9f4cf5111df87da000c29196d3d'}, 
           {'id': '3b982384cf5111df87da000c29196d3d'}, 
           {'id': '3f6f3fcecf5111df87da000c29196d3d'}, 
           {'id': '44762370cf5111df87da000c29196d3d'}, 
           {'id': '4ba0d294cf5111df87da000c29196d3d'})

How can I easily check if a given id is in this list or not?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你又不是我 2024-10-03 23:14:03

这是一句俏皮话:

if some_id in [d.get('id') for d in id_list]:
    pass

虽然效率不是很高。

编辑 - 更好的方法可能是:

if some_id in (d.get('id') for d in id_list):
    pass

这样,列表就不会预先生成完整长度。

Here's a one-liner:

if some_id in [d.get('id') for d in id_list]:
    pass

Not very efficient though.

edit -- A better approach might be:

if some_id in (d.get('id') for d in id_list):
    pass

This way, the list isn't generated in full length beforehand.

末が日狂欢 2024-10-03 23:14:03

如何轻松检查给定 ID 是否在此列表中?

做一套

keys = set( d['id'] for d in id_list )
if some_value in keys

不要问这是否“有效”或“最好”。它涉及标准权衡。

构建场景需要时间。但查找是即时的。

  • 如果您进行大量查找,则构建集合的成本将分摊到每次查找上。

  • 如果你很少进行查找,构建集合的成本可能会比类似的东西更高
    {'id':some_value} in id_list

How can I easily check if a given id is in this list or not?

Make a set

keys = set( d['id'] for d in id_list )
if some_value in keys

Don't ask if this is "efficient" or "best". It involves the standard tradeoff.

Building the set takes time. But the lookup is then instant.

  • If you do a lot of lookups, the cost of building the set is amortized over each lookup.

  • If you do few lookups, the cost of building the set may be higher than something ilike
    {'id':some_value} in id_list.

儭儭莪哋寶赑 2024-10-03 23:14:03

如果你为你的搜索ID制作一个字典,

search_dic = {'id': '0c871320cf5111df87da000c29196d3d'}

id_list = ({'id': '0c871320cf5111df87da000c29196d3d'}, 
           {'id': '2eeeb9f4cf5111df87da000c29196d3d'}, 
           {'id': '3b982384cf5111df87da000c29196d3d'}, 
           {'id': '3f6f3fcecf5111df87da000c29196d3d'}, 
           {'id': '44762370cf5111df87da000c29196d3d'}, 
           {'id': '4ba0d294cf5111df87da000c29196d3d'})


if search_dic in id_list:
    print 'yes'

if you make a dictionary of your search id,

search_dic = {'id': '0c871320cf5111df87da000c29196d3d'}

id_list = ({'id': '0c871320cf5111df87da000c29196d3d'}, 
           {'id': '2eeeb9f4cf5111df87da000c29196d3d'}, 
           {'id': '3b982384cf5111df87da000c29196d3d'}, 
           {'id': '3f6f3fcecf5111df87da000c29196d3d'}, 
           {'id': '44762370cf5111df87da000c29196d3d'}, 
           {'id': '4ba0d294cf5111df87da000c29196d3d'})


if search_dic in id_list:
    print 'yes'
any(x.get('id')==given_id for x in id_list)

。 。 。返回布尔值。效率?参见 S.Lott 的回答

any(x.get('id')==given_id for x in id_list)

. . . returns boolean. Efficiency? See S.Lott's answer

挖鼻大婶 2024-10-03 23:14:03

您可以使用列表理解将其展平并用于:

id in [d['id'] for d in id_list]

您还可以使用生成器表达式,它们具有不同的性能特征(如果您的列表很大,则将使用更少的内存):

id in (d['id'] for d in id_list)

You can flatten it with a list comprehension and use in:

id in [d['id'] for d in id_list]

You can also use generator expressions, which have different performance characteristics (and will use less memory if your list is huge):

id in (d['id'] for d in id_list)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文