Make a dictionary with duplicate keys in Python

后端 未结 7 1071
野的像风
野的像风 2020-11-22 00:37

I have the following list which contains duplicate car registration numbers with different values. I want to convert it into a dictionary which accepts this multiple keys of

7条回答
  •  伪装坚强ぢ
    2020-11-22 00:50

    You can refer to the following article: http://www.wellho.net/mouth/3934_Multiple-identical-keys-in-a-Python-dict-yes-you-can-.html

    In a dict, if a key is an object, there are no duplicate problems.

    For example:

    class p(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return self.name
        def __str__(self):
            return self.name
    d = {p('k'): 1, p('k'): 2}
    

提交回复
热议问题