简体   繁体   English

如果字典的键在frozensets中,则检索值

[英]Retrieving values if keys of the dictionary are in frozensets

I'm using frozensets to keep the keys of my dictionary to take an advantage of union, difference and intersection operations. 我正在使用frozensets来保持我的字典的键,以利用联合,差异和交叉操作。 But when I'm trying to retrieve values by keys from the dictionary through dict.get() it yields a None value. 但是当我试图通过字典中的键通过dict.get()来检索值时,它会产生一个None值。

newDict = {'a': 1, 'b': 2, 'c': 3, 'd': True}
stKeys = set(newDict)
stA = frozenset('a')
stB = frozenset('b')
stC = frozenset('c')
stD = frozenset('d')

print(stKeys)
print(newDict.get(stA & stKeys))
print(newDict.get(stB & stKeys))
print(newDict.get(stC & stKeys))
print(newDict.get(stD & stKeys))

Produce: 生产:

>>>None
>>>None
>>>None
>>>None

And even: 乃至:

print(newDict.get(stA))
print(newDict.get(stB))
print(newDict.get(stC))
print(newDict.get(stD))

Produce: 生产:

>>>None
>>>None
>>>None
>>>None

How to retrieve values from the dictionary if your keys are in frozensets? 如果您的密钥在frozensets中,如何从字典中检索值?

Thanks to Martijn Pieters ! 感谢Martijn Pieters The answer is DVO (Dictionary view objects) and the generator expression if you want to add the result to a list() 答案是DVO(字典视图对象)和生成器表达式,如果你想将结果添加到列表()

You can use dictionary view objects if you want to look for set intersections : 如果要查找集合交叉点,可以使用字典视图对象

for key in newDict.viewkeys() & stA:
    # all keys that are in the intersection of stA and the dictionary

In Python 3, returning dictionary views is the default; 在Python 3中,返回字典视图是默认的; you can use newDict.keys() here: 你可以在这里使用newDict.keys()

for key in newDict.keys() & stA:
    # all keys that are in the intersection of stA and the dictionary

Demo on Python 3: Python 3上的演示:

>>> newDict = {'a': 1, 'b': 2, 'c': 3, 'd': True}
>>> stA = frozenset('a')
>>> stB = frozenset('b')
>>> stC = frozenset('c')
>>> stD = frozenset('d')
>>> newDict.keys() & stA
{'a'}
>>> for key in newDict.keys() & stA:
...     print(newDict[key])
... 
1

To create frozen set keys you need to actually create frozen sets and use them as keys: 要创建冻结集密钥,您需要实际创建冻结集并将其用作键:

newDict = {
    frozenset('a'): 1,
    frozenset('b'): 2,
    frozenset('c'): 3,
    frozenset('d'): True
}

Test: 测试:

>>> {frozenset('a'):1}[frozenset('a')]
1

You actually can do what you are trying to do, at least in 3.6.1 and I suspect 2.7.x as well: 你实际上可以做你想做的事情,至少在3.6.1中我也怀疑2.7.x:

newDict = {frozenset('a'): 1, frozenset('b'): 2, frozenset('c'): 3, 'd': True}
stKeys = set(newDict)
stA = frozenset('a')
print(stA)
stB = frozenset('b')
stC = frozenset('c')
stD = 'd'

print(newDict[stA])
print(newDict[stB])
print(newDict[stC])
print(newDict[stD])

Output: 输出:

frozenset({'a'})
1
2
3
True

The problem seems to be that the key was being assigned as a string object and not a frozenset, yet the search was set to look for a frozenset. 问题似乎是密钥被指定为字符串对象而不是冻结集,但搜索被设置为寻找冻结集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM