-2
{'a':[0,1], 'b':[1,2], 'c':[2,3], 'd':[10,11], 'e':[11,12], 'f':[12,13]}
目的は、接続された値を辞書で見つけることです。 だから、結果は順序は重要ではありません、次のようになります。辞書の値の内容に基づいて接続キーを見つける
{1:['a','b','c'], 2:['d','e','f']}
を私は多くの条件と再帰でループを試してみました。 しかし、さらに混乱してしまった。動作しますが、接続されている値の重複を返す
ループ例、:
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
再帰、全く動作しません:
def recursion(a, data):
for d in data:
if is_connected(a, d):
print d
a = d
recursion(a, data)
辞書を呼び出し中に注文されていません。 https://stackoverflow.com/questions/6083531/order-of-keys-in-python-dictionary – 16num