I have asked a question before about this bit of code and it was answered adequately, but I have an additional question about showing the ten most overdue numbers. (This program was a part of an in-class activity, I have already received full marks for what we have done. The question I am posting here was originally going to be done in-class but we ran out of time in lecture.)
#PE 8
#11-2-17
def main():
#import data
winninglist=get_data()
#find frequency for lotterynumber 1-69
frequency=find_frequency(winninglist)
#sort the frequency
sortedlist=sorting(frequency)
print("The 10 most common numbers and their corresponding frequencies are: ")
print(sortedlist[:10])
print("The 10 least common numbers and their corresponding frequencies are: ")
print(sortedlist[-10:])
#find the 10 most overdue numbers
#find the frequency of 1-69 for the regular numbers, and 1-26 for powerball
def get_data():
#read from the file
infile=open('power.txt','r')
lines=infile.readlines()
infile.close()
#initialize winninglist
winninglist=[]
#processraw data line by line, taking away new character lines, split using space, add to winninglist
for i in range(len(lines)):
lines[i]=lines[i].rstrip('
')
item=lines[i].split()
winninglist+=item
return winninglist
def find_frequency(winninglist):
#frequency should be a list
frequency=[0]*69
#count the occurance of each number
for i in range(69):
for item in winninglist:
if int(item)==(i+1):
frequency[i]+=1
#print(frequency)
return frequency
def sorting(frequency):
#record both the number and frequency
pb_frequency=[]
for i in range(len(frequency)):
pb_frequency.append([i+1, frequency[i]])
#print(pb_frequency)
#now sort using bubble sorting
for i in range(len(pb_frequency)):
max=i
for j in range(i+1, (len(pb_frequency))):
if pb_frequency[j][1]>pb_frequency[max][1]:
max=j
#max has the index of the highest frequency number for this round
#we make the exchange to bubble the largest one
temp1=pb_frequency[i][0]
temp2=pb_frequency[i][1]
pb_frequency[i][0]=pb_frequency[max][0]
pb_frequency[i][1]=pb_frequency[max][1]
pb_frequency[max][0]=temp1
pb_frequency[max][1]=temp2
#print(pb_frequency)
return pb_frequency
main()
Here is the format of the txt file:
17 22 36 37 52 24
14 22 52 54 59 04
05 08 29 37 38 24
10 14 30 40 51 01
(the txt file has many lines of numbers like these)
I need help showing the 10 most overdue numbers. Each line has an implied date attached to it, the first line being the oldest, so, I would need code to show the 10 numbers that have occurred least recently. Is there a simple solution that I am just not seeing?
For reference, here is my last question and answer
Can you help me fix code to show the occurrence of items in a list, for only the first five items?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…