在Python中遍历字典时,为什么必须调用.items()?

问题:在Python中遍历字典时,为什么必须调用.items()?

为什么需要调用items()以遍历字典中的键,值对?即。

dic = {'one': '1', 'two': '2'}
for k, v in dic.items():
    print(k, v)

为什么不是在字典上进行迭代的默认行为

for k, v in dic:
    print(k, v)

Why do you have to call items() to iterate over key, value pairs in a dictionary? ie.

dic = {'one': '1', 'two': '2'}
for k, v in dic.items():
    print(k, v)

Why isn’t that the default behavior of iterating over a dictionary

for k, v in dic:
    print(k, v)

回答 0

对于每个python容器C,期望是

for item in C:
    assert item in C

会顺利通过- 如果一种感觉(循环子句)与另一种感觉(存在检查)完全不同,会不会感到惊讶in?我一定会的!它自然适用于列表,集合,元组,…

因此,当C是一个字典时,如果infor循环生成键/值元组,那么,根据最小惊讶的原理,in还必须将其元组作为其包含检查中的左操作数。

那会有用吗?好看不中用的确,基本上做if (key, value) in C的代名词if C.get(key) == value-这是一张支票,我相信我可能已经执行,或要执行,100倍以上的很少比if k in C实际手段,检查钥匙的存在,完全无视值。

另一方面,只在键上循环很常见,例如:

for k in thedict:
    thedict[k] += 1

拥有价值也无济于事:

for k, v in thedict.items():
    thedict[k] = v + 1

实际上有点不太清晰和简洁。(请注意,这items是用于获取键/值对的“正确”方法的原始拼写:不幸的是,这是在此类访问器返回整个列表的时代,因此,为了支持“公正迭代”,必须引入替代拼写,并且iteritems 在-Python 3中,与以前的Python版本的向后兼容性约束被大大削弱,后来items又变成了)。

For every python container C, the expectation is that

for item in C:
    assert item in C

will pass just fine — wouldn’t you find it astonishing if one sense of in (the loop clause) had a completely different meaning from the other (the presence check)? I sure would! It naturally works that way for lists, sets, tuples, …

So, when C is a dictionary, if in were to yield key/value tuples in a for loop, then, by the principle of least astonishment, in would also have to take such a tuple as its left-hand operand in the containment check.

How useful would that be? Pretty useless indeed, basically making if (key, value) in C a synonym for if C.get(key) == value — which is a check I believe I may have performed, or wanted to perform, 100 times more rarely than what if k in C actually means, checking the presence of the key only and completely ignoring the value.

On the other hand, wanting to loop just on keys is quite common, e.g.:

for k in thedict:
    thedict[k] += 1

having the value as well would not help particularly:

for k, v in thedict.items():
    thedict[k] = v + 1

actually somewhat less clear and less concise. (Note that items was the original spelling of the “proper” methods to use to get key/value pairs: unfortunately that was back in the days when such accessors returned whole lists, so to support “just iterating” an alternative spelling had to be introduced, and iteritems it was — in Python 3, where backwards compatibility constraints with previous Python versions were much weakened, it became items again).


回答 1

我的猜测:使用完整的元组进行循环会更直观,但使用进行成员资格测试可能会更不那么直观in

if key in counts:
    counts[key] += 1
else:
    counts[key] = 1

如果您必须同时指定key和value,那么该代码将无法正常工作in。我很难想象用例,您将检查键和值是否都在字典中。仅测试密钥更为自然。

# When would you ever write a condition like this?
if (key, value) in dict:

现在,in操作员和for ... in操作员不必对相同的项目进行操作。在实现方面,它们是不同的操作(__contains__vs. __iter__)。但是,这种微小的不一致会造成一些混乱,而且不一致。

My guess: Using the full tuple would be more intuitive for looping, but perhaps less so for testing for membership using in.

if key in counts:
    counts[key] += 1
else:
    counts[key] = 1

That code wouldn’t really work if you had to specify both key and value for in. I am having a hard time imagining use case where you’d check if both the key AND value are in the dictionary. It is far more natural to only test the keys.

# When would you ever write a condition like this?
if (key, value) in dict:

Now it’s not necessary that the in operator and for ... in operate over the same items. Implementation-wise they are different operations (__contains__ vs. __iter__). But that little inconsistency would be somewhat confusing and, well, inconsistent.