[英]Comparing Values in Dictionary with Multiple Matching Keys
I have two dictionaries which store product ids as the key and timestamps as the value. 我有两个字典,它们将产品ID存储为键,并将时间戳存储为值。 The problem is that I have repeating keys with unique values.
问题是我重复使用唯一值的键。 For example:
例如:
Dict1 | Dict2
ABCDEF: 12:39:00 | ABCDEF: 10:02:00
ABCDEF: 15:45:00 | ABCDEF: 16:40:00
ABCDEF: 18:30:00 | ABCDEF: 20:22:00
(Not actually formatted this way, just a visual representation. My dictionaries consist of thousands of values.) I have compared them using this: (实际上不是格式化,只是一种视觉表示。我的词典包含成千上万个值。)我使用以下方法对它们进行了比较:
comparison = {x: dict1[x] - dict2[x] for x in dict1 if x in dict2}
But this only compares the last key, value that match in each dictionary. 但这仅比较每个字典中匹配的最后一个键,值。 So I get a result of 01:52 (one hour, 52 minutes).
这样我得到的结果是01:52(一小时52分钟)。 How can I include the other keys, values?
如何包含其他键,值?
Edit: Updated to include more code. 编辑:更新以包括更多代码。
dateList = []
filenameList = []
with open('File1.csv', 'r')as csvfile:
filereader = csv.reader(csvfile, delimiter=',')
next(filereader, None) #skip header row
for column in filereader:
# Extract the datetime info as a datetime object to use in timedelta
dateString = datetime.strptime(column[7], '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
dateObject = datetime.strptime(dateString, '%Y-%m-%d %H:%M:%S')
date1.append(dateObject)
# Extract filename
filename = column[1]
filenameList.append(filename)
# Zip the filenames and datetimes into a dictionary
combinedList = dict(zip(filenameList,dateList))
I literally repeat all that for File2 and that's when the comparison comes in. 我从字面上重复File2的所有操作,然后才进行比较。
As nicolishen commented, all keys in a dict must be unique. 正如nicolishen所说,字典中的所有键必须唯一。 For any given key, your dict will only include the last value added to the original pair of lists.
对于任何给定的键,您的字典将只包括添加到原始列表对中的最后一个值。
You'll need a different data structure. 您将需要一个不同的数据结构。 Consider a dict that contains a single entry for each product ID.
考虑一个字典,其中每个产品ID都包含一个条目。 The value for that entry could be a pair of lists, each one containing time stamp info from one of the data files.
该条目的值可以是一对列表,每个列表包含一个数据文件中的时间戳信息。
productids_timestamps = {'ABCDEF':
(('12:39:00','15:45:00','18:30:00'), # File1.csv
('10:02:00','16:40:00','20:22:00'))} # File2.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.