{'a':[0,1], 'b':[1,2], 'c':[2,3], 'd':[10,11], 'e':[11,12], 'f':[12,13]}
The goal is to find connected values in the dictionary. So, the result would be, order does not matter:
{1:['a','b','c'], 2:['d','e','f']}
I've tried loop with many conditions and recursion. But got even more confused only.
Loop example, which works, but returns duplicates of connected values:
def used(a, data):
try:
for key in data.keys():
if a in data[key]:
return True
else:
return False
except:
return False
def is_connected(a_data, b_data):
if [a for a in a_data if a in b_data]:
return True
else:
return False
collection = {}
key = 1
get_init = True
for i in edges:
for e in edges:
if used(e, collection):
continue
if get_init:
init = e
chain = []
chain.append(init)
get_init = False
continue
else:
pass
if is_connected(init, e):
chain.append(e)
init = e
else:
continue
collection[key] = chain
key += 1
get_init = True
Recursion, that doesn't work at all:
def recursion(a, data):
for d in data:
if is_connected(a, d):
print d
a = d
recursion(a, data)