Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

python - iter, values, item in dictionary does not work

Having this python code

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:UsersEDesktopc.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using Python 3; use dict.items() instead.

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.

From the Python 3 What's New documentation:

  • dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).
  • Also, the dict.iterkeys(), dict.iteritems() and dict.itervalues() methods are no longer supported.

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:

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.

You'll also have to rename the next variable in the while loop; using that replaces the built-in next() function which you need here. Use next_ instead.

The next problem is that you are trying to use current as a key in cycles, but current is a tuple of an integer and a list of integers, making the whole value not hashable. I think you wanted to get just the next key instead, in which case next(iter(dict)) would give you that:

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]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...