I have two dictionaries which store product ids as the key and timestamps as the value. 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). 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.
As nicolishen commented, all keys in a dict must be unique. 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.