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?
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.
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.
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.
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.
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
methodsdict.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.
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]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With