Python iter,值,字典中的项目不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20444340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iter, values, item in dictionary does not work
提问by cMinor
Having this python code
有这个python代码
edges = [(0, [3]), (1, [0]), (2, [1, 6]), (3, [2]), (4, [2]), (5, [4]), (6, [5, 8]), (7, [9]), (8, [7]), (9, [6])]
graph = {0: [3], 1: [0], 2: [1, 6], 3: [2], 4: [2], 5: [4], 6: [5, 8], 7: [9], 8: [7], 9: [6]}
cycles = {}
while graph:
current = graph.iteritems().next()
cycle = [current]
cycles[current] = cycle
while current in graph:
next = graph[current][0]
del graph[current][0]
if len(graph[current]) == 0:
del graph[current]
current = next
cycle.append(next)
def traverse(tree, root):
out = []
for r in tree[root]:
if r != root and r in tree:
out += traverse(tree, r)
else:
out.append(r)
return out
print ('->'.join([str(i) for i in traverse(cycles, 0)]))
Traceback (most recent call last):
File "C:\Users\E\Desktop\c.py", line 20, in <module>
current = graph.iteritems().next()
AttributeError: 'dict' object has no attribute 'iteritems'
I also tried itervalues, iterkeys... but that does not work How to modify code?
我也试过 itervalues、iterkeys ......但那不起作用 如何修改代码?
采纳答案by Martijn Pieters
You are using Python 3; use dict.items()instead.
您正在使用 Python 3;使用dict.items()来代替。
The Python 2 dict.iter*methods have been renamed in Python 3, where dict.items()returns a dictionary view instead of a list by default now. Dictionary views act as iterables in the same way dict.iteritems()do in Python 2.
Python 2dict.iter*方法已在 Python 3 中重命名,dict.items()现在默认返回字典视图而不是列表。字典视图以与dict.iteritems()Python 2 中相同的方式充当可迭代对象。
From the Python 3 What's New documentation:
dictmethodsdict.keys(),dict.items()anddict.values()return “views” instead of lists. For example, this no longer works:k = d.keys(); k.sort(). Usek = sorted(d)instead (this works in Python 2.5 too and is just as efficient).- Also, the
dict.iterkeys(),dict.iteritems()anddict.itervalues()methods are no longer supported.
dictmethodsdict.keys(),dict.items()并dict.values()返回“视图”而不是列表。例如,这不再有效:k = d.keys(); k.sort(). 使用k = sorted(d)替代(在Python 2.5太这个作品也一样可以有效)。- 此外,不再支持
dict.iterkeys(),dict.iteritems()和dict.itervalues()方法。
Also, the .next()method has been renamed to .__next__(), but dictionary views are not iterators. The line graph.iteritems().next()would have to be translated instead, to:
此外,该.next()方法已重命名为.__next__(),但字典视图不是迭代器。该行graph.iteritems().next()必须改为:
current = next(iter(graph.items()))
which uses iter()to turn the items view into an iterable and next()to get the next value from that iterable.
它用于iter()将项目视图转换为可迭代对象并next()从该可迭代对象中获取下一个值。
You'll also have to rename the nextvariable in the whileloop; using that replaces the built-in next()function which you need here. Use next_instead.
您还必须重命名循环中的next变量while;使用它替换了next()您在此处需要的内置函数。使用next_来代替。
The next problem is that you are trying to use currentas a key in cycles, but currentis a tuple of an integer and a listof integers, making the whole value not hashable. I thinkyou wanted to get just the next keyinstead, in which case next(iter(dict))would give you that:
下一个问题是您试图将current用作 中的键cycles,但它current是一个整数元组和一个整数列表,使整个值不可散列。我认为您只想获得下一个密钥,在这种情况下next(iter(dict))会给您:
while graph:
current = next(iter(graph))
cycle = [current]
cycles[current] = cycle
while current in graph:
next_ = graph[current][0]
del graph[current][0]
if len(graph[current]) == 0:
del graph[current]
current = next_
cycle.append(next_)
This then produces some output:
这会产生一些输出:
>>> cycles
{0: [0, 3, 2, 1, 0], 2: [2, 6, 5, 4, 2], 6: [6, 8, 7, 9, 6]}

