Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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?

like image 829
edgarmtze Avatar asked Dec 07 '13 17:12

edgarmtze


People also ask

How do I fetch values in a dictionary?

You can use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.

Which one of the following methods cant be used to iterate over items in a dictionary *?

filter() filter() is another built-in function that you can use to iterate through a dictionary in Python and filter out some of its items.

How do you use dictionary values in Python?

Python dictionary | values() values() is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list. If you use the type() method on the return value, you get “dict_values object”. It must be cast to obtain the actual list.

Can you loop through a dictionary Python?

You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.


1 Answers

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]}
like image 157
Martijn Pieters Avatar answered Sep 23 '22 17:09

Martijn Pieters