A list as a key for a dictionary

前端 未结 5 1907
一整个雨季
一整个雨季 2021-01-22 08:28

I have multiple lists of tuples eg

[([1, 2, 3, 4], 2), ([5, 6, 7], 3)]

that I would like to have as keys to a dictionary (so each key in my dic

相关标签:
5条回答
  • 2021-01-22 08:37
    >>> def nested_lists_to_tuples(ls):
        return tuple(nested_lists_to_tuples(l) if isinstance(l, (list, tuple)) else l for l in ls)
    
    >>> nested_lists_to_tuples([([1,2,3,4],2),([5,6,7],3)])
    (((1, 2, 3, 4), 2), ((5, 6, 7), 3))
    

    Then just use that returned value as your key. Note that I did it this way so you could support even more deeply nested mixes of tuples and lists, like [([1,(2, [3, 4, [5, 6, (7, 8)]]), 3, 4], 2), ([5, 6, 7], 3)]:

    >>> nested_lists_to_tuples([([1, (2, [3, 4, [5, 6, (7, 8)]]), 3, 4], 2), ([5, 6, 7], 3)])
    (((1, (2, (3, 4, (5, 6, (7, 8)))), 3, 4), 2), ((5, 6, 7), 3))
    

    There may possibly be a simpler way to do this, though.

    0 讨论(0)
  • 2021-01-22 08:43

    You should convert lists to tuples

    0 讨论(0)
  • 2021-01-22 08:46

    Convert your lists to tuples:

    dict((tuple(a), b) for a,b in [([1,2,3,4],2),([5,6,7],3)])
    

    If you are using Python >= 2.7 you can use dict-comprehensions:

    {tuple(a): b for a,b in [([1,2,3,4],2),([5,6,7],3)]}
    
    0 讨论(0)
  • 2021-01-22 08:48

    Use repr

    class A:
        pass
    
    import time
    
    # A and time as heterogenous elements, only to show the generality of my solution
    
    li_li = [ [([1,2,3,4],2),([5,6,7],3)] ,
              [([10,20,3],2),      ([5,6,77],3)] ,
              [([1,2,3,4],2),([5,6,time,7],3),([875,12], ['jk',78], A, (100,23),'zuum')] ]
    
    
    
    
    didi = {}
    for i,li in enumerate(li_li):
        didi[repr(li)] = i
    
    print 'dictionary  didi:'
    for k,v in didi.iteritems():
        print k,'     ',v
    
    print '----------------------------------'
    
    print didi[repr([([1+1+1+1+1+5,         200000/10000,    3],2),([5,8-2,7*11],3)      ])]
    

    result

    dictionary  didi:
    [([1, 2, 3, 4], 2), ([5, 6, <module 'time' (built-in)>, 7], 3), ([875, 12], ['jk', 78], <class __main__.A at 0x011CFC70>, (100, 23), 'zuum')]       2
    [([1, 2, 3, 4], 2), ([5, 6, 7], 3)]       0
    [([10, 20, 3], 2), ([5, 6, 77], 3)]       1
    ----------------------------------
    1
    
    0 讨论(0)
  • 2021-01-22 08:56

    Use tuples instead.

    >>> dict((tuple(x[0]), x[1]) for x in [([1,2,3,4],2),([5,6,7],3)])
    {(5, 6, 7): 3, (1, 2, 3, 4): 2}
    
    0 讨论(0)
提交回复
热议问题