<legend id='AmnHF'><style id='AmnHF'><dir id='AmnHF'><q id='AmnHF'></q></dir></style></legend>
    <i id='AmnHF'><tr id='AmnHF'><dt id='AmnHF'><q id='AmnHF'><span id='AmnHF'><b id='AmnHF'><form id='AmnHF'><ins id='AmnHF'></ins><ul id='AmnHF'></ul><sub id='AmnHF'></sub></form><legend id='AmnHF'></legend><bdo id='AmnHF'><pre id='AmnHF'><center id='AmnHF'></center></pre></bdo></b><th id='AmnHF'></th></span></q></dt></tr></i><div id='AmnHF'><tfoot id='AmnHF'></tfoot><dl id='AmnHF'><fieldset id='AmnHF'></fieldset></dl></div>

  • <small id='AmnHF'></small><noframes id='AmnHF'>

        <bdo id='AmnHF'></bdo><ul id='AmnHF'></ul>

        <tfoot id='AmnHF'></tfoot>
      1. 在 Python 中创建一个带有重复键的字典

        Make a dictionary with duplicate keys in Python(在 Python 中创建一个带有重复键的字典)
        <legend id='NOjqd'><style id='NOjqd'><dir id='NOjqd'><q id='NOjqd'></q></dir></style></legend>

          <small id='NOjqd'></small><noframes id='NOjqd'>

              • <i id='NOjqd'><tr id='NOjqd'><dt id='NOjqd'><q id='NOjqd'><span id='NOjqd'><b id='NOjqd'><form id='NOjqd'><ins id='NOjqd'></ins><ul id='NOjqd'></ul><sub id='NOjqd'></sub></form><legend id='NOjqd'></legend><bdo id='NOjqd'><pre id='NOjqd'><center id='NOjqd'></center></pre></bdo></b><th id='NOjqd'></th></span></q></dt></tr></i><div id='NOjqd'><tfoot id='NOjqd'></tfoot><dl id='NOjqd'><fieldset id='NOjqd'></fieldset></dl></div>
                  <bdo id='NOjqd'></bdo><ul id='NOjqd'></ul>
                    <tbody id='NOjqd'></tbody>

                  <tfoot id='NOjqd'></tfoot>
                  本文介绍了在 Python 中创建一个带有重复键的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下列表,其中包含具有不同值的重复汽车注册号.我想把它转换成一个接受这多个汽车登记号码键的字典.

                  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 car registration numbers.

                  到目前为止,当我尝试将列表转换为字典时,它消除了其中一个键.如何制作带有重复键的字典?

                  So far when I try to convert list to dictionary it eliminates one of the keys. How do I make a dictionary with duplicate keys?

                  名单是:

                  EDF768, Bill Meyer, 2456, Vet_Parking
                  TY5678, Jane Miller, 8987, AgHort_Parking
                  GEF123, Jill Black, 3456, Creche_Parking
                  ABC234, Fred Greenside, 2345, AgHort_Parking
                  GH7682, Clara Hill, 7689, AgHort_Parking
                  JU9807, Jacky Blair, 7867, Vet_Parking
                  KLOI98, Martha Miller, 4563, Vet_Parking
                  ADF645, Cloe Freckle, 6789, Vet_Parking
                  DF7800, Jacko Frizzle, 4532, Creche_Parking
                  WER546, Olga Grey, 9898, Creche_Parking
                  HUY768, Wilbur Matty, 8912, Creche_Parking
                  EDF768, Jenny Meyer, 9987, Vet_Parking
                  TY5678, Jo King, 8987, AgHort_Parking
                  JU9807, Mike Green, 3212, Vet_Parking
                  

                  我试过的代码是:

                  data_dict = {}
                  data_list = []
                  
                  def createDictionaryModified(filename):
                    path = "C:UsersuserDesktop"
                    basename = "ParkingData_Part3.txt"
                    filename = path + "//" + basename
                    file = open(filename)
                    contents = file.read()
                    print contents,"
                  "
                    data_list = [lines.split(",") for lines in contents.split("
                  ")]
                    for line in data_list:
                      regNumber = line[0]
                      name = line[1]
                      phoneExtn = line[2]
                      carpark = line[3].strip()
                      details = (name,phoneExtn,carpark)
                      data_dict[regNumber] = details
                    print data_dict,"
                  "
                    print data_dict.items(),"
                  "
                    print data_dict.values()
                  

                  推荐答案

                  Python 字典不支持重复键.一种解决方法是将列表或集合存储在字典中.

                  Python dictionaries don't support duplicate keys. One way around is to store lists or sets inside the dictionary.

                  实现此目的的一种简单方法是使用 defaultdict:

                  One easy way to achieve this is by using defaultdict:

                  from collections import defaultdict
                  
                  data_dict = defaultdict(list)
                  

                  你所要做的就是替换

                  data_dict[regNumber] = details
                  

                  data_dict[regNumber].append(details)
                  

                  你会得到一个列表字典.

                  and you'll get a dictionary of lists.

                  这篇关于在 Python 中创建一个带有重复键的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  <i id='s29hb'><tr id='s29hb'><dt id='s29hb'><q id='s29hb'><span id='s29hb'><b id='s29hb'><form id='s29hb'><ins id='s29hb'></ins><ul id='s29hb'></ul><sub id='s29hb'></sub></form><legend id='s29hb'></legend><bdo id='s29hb'><pre id='s29hb'><center id='s29hb'></center></pre></bdo></b><th id='s29hb'></th></span></q></dt></tr></i><div id='s29hb'><tfoot id='s29hb'></tfoot><dl id='s29hb'><fieldset id='s29hb'></fieldset></dl></div>
                  • <small id='s29hb'></small><noframes id='s29hb'>

                      <tbody id='s29hb'></tbody>

                        <bdo id='s29hb'></bdo><ul id='s29hb'></ul>
                      • <tfoot id='s29hb'></tfoot>
                        <legend id='s29hb'><style id='s29hb'><dir id='s29hb'><q id='s29hb'></q></dir></style></legend>