在 Python 中创建一个带有重复键的字典

2024-04-21Python开发问题
13

本文介绍了在 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 中创建一个带有重复键的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10