Make a dictionary with duplicate keys in Python

后端 未结 7 1180
野的像风
野的像风 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 01:08

    You can't have duplicated keys in a dictionary. Use a dict of lists:

    for line in data_list:
      regNumber = line[0]
      name = line[1]
      phoneExtn = line[2]
      carpark = line[3].strip()
      details = (name,phoneExtn,carpark)
      if not data_dict.has_key(regNumber):
        data_dict[regNumber] = [details]
      else:
        data_dict[regNumber].append(details)
    

提交回复
热议问题