How to show the ten most overdue numbers in a list

2024/11/18 4:34:09

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-17def main():#import datawinninglist=get_data()#find frequency for lotterynumber 1-69frequency=find_frequency(winninglist)#sort the frequencysortedlist=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 powerballdef get_data():#read from the fileinfile=open('power.txt','r')lines=infile.readlines()infile.close()#initialize winninglistwinninglist=[]#processraw data line by line, taking away new character lines, split using space, add to winninglistfor i in range(len(lines)):lines[i]=lines[i].rstrip('\n')item=lines[i].split()winninglist+=itemreturn winninglistdef find_frequency(winninglist):#frequency should be a listfrequency=[0]*69#count the occurance of each numberfor i in range(69):for item in winninglist:if int(item)==(i+1):frequency[i]+=1#print(frequency)return frequencydef sorting(frequency):#record both the number and frequencypb_frequency=[]for i in range(len(frequency)):pb_frequency.append([i+1, frequency[i]])#print(pb_frequency)#now sort using bubble sortingfor i in range(len(pb_frequency)):max=ifor 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 onetemp1=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]=temp1pb_frequency[max][1]=temp2#print(pb_frequency)return pb_frequencymain()

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?

Answer
from collections import Counter
input = ['your numbers in format of list']
d = {}for i in input:if i in d:d.update({i: 1+d[i]})else:d.update({i: 1})i = 0
l_min = []while len(Counter(x for sublist in l_min for x in sublist)) < 11:i += 1l_min.append([k for k, v in d.items() if v == i])print(l_min)
https://en.xdnf.cn/q/120112.html

Related Q&A

Connect a Flask webservice from a device which is not on the same network

I am not an expert in web programming and know very little about it. I am trying to run a webservice on an EC2 instance (Windows Server 2012R2) and the webservice is written in Python using Flask packa…

why int object is not iterable while str is into python [duplicate]

This question already has answers here:Why is int" not iterable in Python, but str are?(4 answers)Closed 2 years ago.As i know we can not iterate int value while we can iterate strings in python.…

an irregular anomaly in python tuple

i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run throug…

AttributeError: type object Employee has no attribute Worker

Taking a class on Python coding and trying to use inheritance to code an answer to this problem: Write an Employee class that keeps data attributes for the following piece of information: Employee name…

How to add tag for numbers which in brackets using python regex?

The default strings is:strings123[abc123def456]strings456Add tag for number:strings[abc<span>123</span>def<span>456</span>]strings

How to write program run matrix as below in python?

Thanks for everyones reply. I will explain here. Suppose there is a given matrixx y B = [5,-4,5,-6] [[0,0,0,0], [[0,1,0,1],[0,0,0,0], [0,0,0,0],[0,0,0,0],…

Scraping Dynamic Information

I recently started with coding, I use Python and Pycharm. I Installed and imported the needed "Add-ons" like Selenium. For my first project I tried to get the "address" information …

Combining three RGB images into a single RGB image

I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0s in the blue and green channels) and I want to combine them into a single RGB image with the correct …

Parse XML to Table in Python

Im trying to parse XML to table-like structure in Python. Imagine XML like this:<?xml version="1.0" encoding="UTF-8"?> <base><element1>element 1</element1>…

Use Artificial Intelligence to predict next number (n+1) in a sequence

The AI must predict the next number in a given sequence of incremental integers using Python, but so far I havent gotten the intended result. I tried changing the learning rate and iterations but so fa…