Closed lissa1496 closed 3 years ago
name = input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) dictionary = dict() #creating a dictionary lst = [] #creating a list for line in handle: #for every line in the file words = line.split() #split every word in each line if len(words) > 2 and words[0] == 'From': #if the words list has more than two words and the
hr = words[5].split(':') #save hours in hr and split the hour using the : symbol
dictionary[hr[0]] = dictionary.get(hr[0], 0) + 1 #if that particular hr is present in dictionary
#then add 1 to the current count, else add taht hour to dictionary and make count 1
else:
continue
for k,v in dictionary.items(): #for every item in the dictionary go through in for loop lst.append((k,v)) #add items in dictionary to the list with its key
lst.sort() #sort the list wrt k,key for k,v in lst: #for loop to print the list of keys and values in separate lines print(k,v)
I have been working in this assignment. I've looked different answers, however, I really want to understand all the lines in the assignments. Could someone explain me the lines please! I'm new in Python.
wk9 - assignment 9.4.py Thank you