小编Chi*_*Lim的帖子

Python 3 - 多个字典键的交集

下面是我的代码,任何人都可以通过使用 python 3 内置库函数帮助我优化过程吗?

Dict1 = {'ky1':1, 'ky2':2,'ky_3':3}
Dict2 = {'ky1':4, 'ky2':5,'ky_4':6}
Dict3 = {'ky2':7, 'ky3':8,'ky_5':9}
D = [Dict1,Dict2,Dict3]
Keys_list = []
for i in D:
   tmp = list(i.keys())
   Keys_list.append(tmp)
Output = list(set.intersection(*map(set,Keys_list)))
Run Code Online (Sandbox Code Playgroud)

我的Dict1、Dict2、Dict3 是大字典
谢谢

python dictionary intersection key python-3.x

5
推荐指数
1
解决办法
2284
查看次数

使用键列表从Python字典中收集值

Dict = {'w1': 56, 'w2': 19, 'w3': 77, 'w4': 45, 'w5': 31}
myWords = ['w1','w4','w5']

OutputList=[]
for items in myWords:
    tmps = Dict[items]
    OutputList.append(tmps)
Run Code Online (Sandbox Code Playgroud)

我的问题是,我们可以在使用for循环的情况下从具有特定列表("myWord")的字典中收集值(输出)吗?

python dictionary key function

2
推荐指数
3
解决办法
1986
查看次数

标签 统计

dictionary ×2

key ×2

python ×2

function ×1

intersection ×1

python-3.x ×1