iterate over list of tuple get error: unhashable type: 'slice' [closed]

孤人 提交于 2020-01-17 18:02:01

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!