I have a list of answers as follows:
answers = defaultdict(, {38: [0, 17], 19: [2], 5: [14], 3: [21], 1: [23, 32], 25: [26], 42: [28, 40], 34: [31], 12: [43, 46]})
Now I'm trying to connect the points as follows:
for v in answers.values():
if len(v)>1:
T.add_edge(v[0],v[1])
This works fine, but when the length of answers.values() is 1, like in (...., 19: [2], ...) I need to connect 2 with its corresponding class value (which is 19). How can I access the class value in the above answers list?
解决方案
You will need to keep track of the keys and the value in that dict like:
for k, v in answers.items():
if len(v) > 1:
T.add_edge(v[0], v[1])
else:
T.add_edge(k, v[0])