max*_*max 8 python dictionary python-3.x
是否有任何习惯用于从字典中获取任意键,值对而不删除它们?(P3K)
编辑:
抱歉这个令人困惑的措辞.
在某种意义上我使用了任意一词,我不关心我得到的东西.
它与随机不同,我关心的是我得到的东西(即,我需要选择每个项目的概率相同).
我没有钥匙可以使用; 如果我这样做,我认为它将属于RTFM类别,并且不值得回答SO.
编辑:
不幸的是在P3K中,.items()
返回一个dict_items
对象,不像Python 2返回了一个迭代器:
ActivePython 3.1.2.4 (ActiveState Software Inc.) based on
Python 3.1.2 (r312:79147, Sep 14 2010, 22:00:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {1:2}
>>> k,v = next(d.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dict_items object is not an iterator
Run Code Online (Sandbox Code Playgroud)
k, v = next(iter(d.items())) # updated for Python 3
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2152 次 |
最近记录: |