0

Tuples and dicts.

ok I dont see my error ... beginning to hit too many roadblocks no matter how many extra practice I do. I have this working all the way through and as far as I can tell it should be doing exactly what is needed. I need my output to be this: 04 3 06 1 07 1 09 2 10 3 11 6 14 1 15 2 16 4 17 2 18 1 19 1 Except I Keep getting mine in random order despite sorting and everything else the numbers are right just wont sort. I get this. 11 6 16 4 10 3 04 3 17 2 15 2 09 2 19 1 18 1 14 1 07 1 06 1 fname = input("Enter file name: ") fh = open(fname) count = dict() for handle in fh: if not handle.startswith("From "): continue #print(handle) time = handle.split() #creating new list time2 = time[5] #print(time2) time3 = time2.split(':') #print(time3) time4 = time3[0] #print(time4) count[time4] = count.get(time4,0) + 1 #print(time4,count[time4]) timelst = list() for timekey,valuekey in list(count.items()): timelst.append((valuekey,timekey)) timelst.sort(reverse=True) for valuekey,timekey in timelst: print(timekey,valuekey)

8th Nov 2017, 2:23 PM
Aric Dunn
Aric Dunn - avatar
8 Antworten
+ 3
What happens if you ignore the instructions and switch valuekey and timekey in the append statement?
8th Nov 2017, 3:53 PM
Paul
Paul - avatar
+ 1
I believe it is sorted, but it is sorted on the second column first, and the first column second. Where you append, why in the order valuekey, timekey and not the other way around?
8th Nov 2017, 3:41 PM
Paul
Paul - avatar
+ 1
So it works now?
8th Nov 2017, 4:00 PM
Paul
Paul - avatar
0
That is how the instructions say to do it. I am stuck at this last little part and cant figure out whats wrong.. from the looks of the needed output the times are sorted from lowest to highest. here is the question Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below
8th Nov 2017, 3:45 PM
Aric Dunn
Aric Dunn - avatar
0
I think it wont sort because of the zero in front of some of the hours. but I am dont know how to resolve that.
8th Nov 2017, 3:48 PM
Aric Dunn
Aric Dunn - avatar
0
you get the value in the wrong column. needs to be sorted by the hour ( value) not the key 1 19 1 18 2 17 4 16 2 15 1 14 6 11 3 10 2 09 1 07 1 06 3 04
8th Nov 2017, 3:56 PM
Aric Dunn
Aric Dunn - avatar
0
haha.. literary works if you do the opposite of the instructions. has to switch everything around.. soooo ridiculous. TY :)
8th Nov 2017, 3:58 PM
Aric Dunn
Aric Dunn - avatar
0
yes.. I have been beating myself up since yesterday morning... soooo annoying. fname = input("Enter file name: ") fh = open(fname) count = dict() for handle in fh: if not handle.startswith("From "): continue #print(handle) time = handle.split() #creating new list time2 = time[5] #print(time2) time3 = time2.split(':') #print(time3) time4 = time3[0] #print(time4) count[time4] = count.get(time4,0) + 1 #print(time4,count[time4]) timelst = list() for timekey,valuekey in list(count.items()): timelst.append((timekey,valuekey)) timelst.sort() for valuekey,timekey in timelst: print(valuekey,timekey)
8th Nov 2017, 4:02 PM
Aric Dunn
Aric Dunn - avatar