Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d = [('A', 'B', 1), ('C', 'D', 1),
- ('B', 'D', 2), ('A', 'B', 3),
- ('A', 'D', 3), ('B', 'C', 4),
- ('A', 'C', 5), ('B', 'C', 8)]
- d = [('A', 'B', 1), ('C', 'D', 1),
- ('B', 'D', 2), ('A', 'D', 3),
- ('B', 'C', 4), ('A', 'C', 5)]
- edge_dict = {}
- for x in d:
- key = '%s%s' % (x[0], x[1])
- if not edge_dict.get(key):
- edge_dict[key] = x[2]
- else:
- if edge_dict[key] > x[2]:
- edge_dict[key] = x[2]
- final_list = []
- for k, v in edge_dict.items():
- t = list(k)
- t.append(v)
- final_list.append(tuple(t))
- final_list.sort(key=lambda x: x[2])
- print final_list
- import itertools
- d = [('A', 'B', 1), ('C', 'D', 1),
- ('B', 'D', 2), ('A', 'B', 3),
- ('A', 'D', 3), ('B', 'C', 4),
- ('A', 'C', 5), ('B', 'C', 8)]
- new_d = [min(list(b), key=lambda x:x[-1]) for _, b in itertools.groupby(sorted(d, key=lambda x:x[:-1]), key=lambda x:x[:-1])]
- [('A', 'B', 1), ('A', 'C', 5), ('A', 'D', 3), ('B', 'C', 4), ('B', 'D', 2), ('C', 'D', 1)]
Add Comment
Please, Sign In to add comment