查找一些词典的通用键

编程入门 行业动态 更新时间:2024-10-27 17:23:07
本文介绍了查找一些词典的通用键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有大约20000个字典。我想找到所有字典中都通用的所有键(该键必须在所有20000个字典中都存在)。如何实现这一目标。目前,我正在写类似的东西,但是它没有给我期望的结果:

I have around 20000 dictionaries.I want find all the keys which are common in all the dictionary(that key need to be present in all 20000) dictionary.How can I achieve this.Currently I am writing something like that,But its not giving me the desired result:

if parsedData.keys() not in uniquelist: uniquelist.append(parsedData.keys()) else: commonlist.append(parsedData.keys())

这里解析的数据是我的字典。

here parsed data is my dictionary. Please help me with this.

推荐答案

您可以在第一个字典中创建一组键,然后以循环形式创建

You could create the set of the keys in the first dictionary and then in a loop form the intersection with the remaining keys:

>>> d1 = {'a':1, 'b':2, 'c':3} >>> d2 = {'b':4, 'c':7,'d':5} >>> d3 = {'b':5, 'c':8,'e':6} >>> dicts = [d1,d2,d3] >>> common_keys = set(d1.keys()) >>> for d in dicts[1:]: common_keys.intersection_update(set(d.keys())) >>> common_keys {'b', 'c'}

更多推荐

查找一些词典的通用键

本文发布于:2023-11-04 21:49:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词典

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!