为什么在python中迭代字典时必须调用.iteritems() ?

时间:2021-09-18 01:20:01

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

为什么必须调用iteritems()来在字典中遍历键、值对?即

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

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

为什么这不是遍历字典的默认行为呢

for k, v in dic:
    print k, v

2 个解决方案

#1


169  

For every python container C, the expectation is that

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

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.

因此,当C是一个字典时,如果in要在for循环中生成键/值元组,那么,按照最小惊奇的原则,in也必须在包含检查中取这样一个元组作为它的左操作数。

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.

这会有多大用处呢?确实相当无用的,基本上如果(键,值)C如果C.get的同义词(关键)= =价值——这是一个检查我相信我可能执行,或想要执行,很少100倍如果k C实际上意味着什么,只检查关键的存在,完全忽略了价值。

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).

实际上不太清晰,也不太简洁。(注意,商品的原拼法是“正确”的方法来使用键/值对:不幸的是那是回到过去的时光,当这种访问器返回整个列表,所以支持“迭代”另一种拼写必须介绍,和iteritems——在Python 3,与先前的Python版本,向后兼容性约束弱得多,它再次成为项目)。

#2


9  

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

我的猜测是:使用完整的tuple可以更直观地进行循环,但是对于使用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.

如果必须同时指定in的键和值,那么该代码就不能正常工作。我很难想象用例,在这里你会检查键和值是否都在字典中。只测试键要自然得多。

# 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.

现在没有必要让in运算符。在同一项目上操作。实现方面,它们是不同的操作(__contains__和__iter__)。但这种小小的不一致会让人感到困惑,而且,呃,不一致。

#1


169  

For every python container C, the expectation is that

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

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.

因此,当C是一个字典时,如果in要在for循环中生成键/值元组,那么,按照最小惊奇的原则,in也必须在包含检查中取这样一个元组作为它的左操作数。

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.

这会有多大用处呢?确实相当无用的,基本上如果(键,值)C如果C.get的同义词(关键)= =价值——这是一个检查我相信我可能执行,或想要执行,很少100倍如果k C实际上意味着什么,只检查关键的存在,完全忽略了价值。

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).

实际上不太清晰,也不太简洁。(注意,商品的原拼法是“正确”的方法来使用键/值对:不幸的是那是回到过去的时光,当这种访问器返回整个列表,所以支持“迭代”另一种拼写必须介绍,和iteritems——在Python 3,与先前的Python版本,向后兼容性约束弱得多,它再次成为项目)。

#2


9  

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

我的猜测是:使用完整的tuple可以更直观地进行循环,但是对于使用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.

如果必须同时指定in的键和值,那么该代码就不能正常工作。我很难想象用例,在这里你会检查键和值是否都在字典中。只测试键要自然得多。

# 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.

现在没有必要让in运算符。在同一项目上操作。实现方面,它们是不同的操作(__contains__和__iter__)。但这种小小的不一致会让人感到困惑,而且,呃,不一致。