问题
i have a list of tuple like this

seq=[((), ('2',)), (('0', '1'), ('1', '4')), (('0', '2'), ('2', '0')), (('0', '2'), ('2', '4')),(('0', '4'), ('4', '0')), (('0', '4'), ('4', '2')), (('1', '2'), ('2', '4')), (('1', '4'), ('4', '1')), (('2',), ('2', '0')), (('2', '0'), ('0', '2')), (('2', '0'), ('0', '4')),(('2', '1'), ('1', '2')), (('2', '3'), ('3', '2')), (('2', '4'), ('4', '2')), (('3', '2'),('2', '0')), (('4', '0'), ('0', '1')), (('4', '0'), ('0', '2')), (('4', '0'), ('0', '4')), (('4', '1'), ('1', '2')), (('4', '1'), ('1', '4')), (('4', '2'), ('2', '0')), (('4', '2'), ('2', '1')), (('4', '2'), ('2', '3')), (('4', '2'), ('2', '4'))]
the elements could concentrate together to form a chain, for example:
((), ('2',))->(('2', '0'), ('0', '4'))->(('4', '2'), ('2', '1'))
,
the rule is like this:
if the last element of the tuple is the same as the first element of the next tuple, then these two could be concentrated:((), ('2',))->(('2', '0'), ('0', '4'))->(('4', '2'), ('2', '1'))
I write a function like this, which is not working:
def find_chain(seq):
agg={}
for key1, key2 in seq:
if key1[1][1]==key2[0][0]:
agg.add(key1, key2)
and gives me such error: unhashable type: 'slice'
seq = [((), ('2',)),
(('0', '1'), ('1', '4')),
(('0', '2'), ('2', '0')),
(('0', '2'), ('2', '4')),
(('0', '4'), ('4', '0')),
(('0', '4'), ('4', '2')),
(('1', '2'), ('2', '4')),
(('1', '4'), ('4', '1')),
(('2',), ('2', '0')),
(('2', '0'), ('0', '2')),
(('2', '0'), ('0', '4')),
(('2', '1'), ('1', '2')),
(('2', '3'), ('3', '2')),
(('2', '4'), ('4', '2')),
(('3', '2'), ('2', '0')),
(('4', '0'), ('0', '1')),
(('4', '0'), ('0', '2')),
(('4', '0'), ('0', '4')),
(('4', '1'), ('1', '2')),
(('4', '1'), ('1', '4')),
(('4', '2'), ('2', '0')),
(('4', '2'), ('2', '1')),
(('4', '2'), ('2', '3')),
(('4', '2'), ('2', '4'))]
来源:https://stackoverflow.com/questions/59706259/iterate-over-list-of-tuple-get-error-unhashable-type-slice