Python creating dictionary key from a list of items

前端 未结 2 1497
离开以前
离开以前 2021-01-22 09:56

I wish to use a Python dictionary to keep track of some running tasks. Each of these tasks has a number of attributes which makes it unique, so I\'d like to use a function of th

相关标签:
2条回答
  • 2021-01-22 10:27

    Use a tuple in place of the list. Tuples are immutable and can be used as dictionary keys:

    d[(attrib_a, attrib_b)] = t
    

    The parentheses can be omitted:

    d[attrib_a, attrib_b] = t
    

    However, some people seem to dislike this syntax.

    0 讨论(0)
  • 2021-01-22 10:32

    Use a tuple

    d[(attrib_a, attrib_b)] = t
    

    That should work fine

    0 讨论(0)
提交回复
热议问题