Python 如何显示列表中最过期的十个数字
我曾经问过一个关于这段代码的问题,并且得到了充分的回答,但是我还有一个关于显示十个最过期的数字的问题。(本课程是课堂活动的一部分,我已经获得了满分。我在这里发布的问题原本是在课堂上完成的,但我们在课堂上没有时间了。) 以下是txt文件的格式: 1722236375224 142252545904 05 08 29 37 38 24 101430401 (txt文件中有许多这样的数字行) 我需要帮助显示10个最过期的数字。每一行都有一个隐含的日期,第一行是最早的,因此,我需要代码来显示最近发生的10个数字。有没有一个简单的解决方案,我只是没有看到 作为参考,这里是我最后的问题和答案Python 如何显示列表中最过期的十个数字,python,Python,我曾经问过一个关于这段代码的问题,并且得到了充分的回答,但是我还有一个关于显示十个最过期的数字的问题。(本课程是课堂活动的一部分,我已经获得了满分。我在这里发布的问题原本是在课堂上完成的,但我们在课堂上没有时间了。) 以下是txt文件的格式: 1722236375224 142252545904 05 08 29 37 38 24 101430401 (txt文件中有许多这样的数字行) 我需要帮助显示10个最过期的数字。每一行都有一个隐含的日期,第一行是最早的,因此,我需要代码来显示最近发生的1
来自集合导入计数器
input=['列表格式的数字']
d={}
对于输入中的i:
如果我是d:
d、 更新({i:1+d[i]})
其他:
d、 更新({i:1})
i=0
l_min=[]
而len(计数器(x表示子列表中的x,最小值表示子列表中的x))<11:
i+=1
l_min.append([k代表k,v在d.items()中,如果v==i])
打印(l_min)
这看起来像是一个家庭作业问题,但没有被识别为正确识别的问题
#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('\n')
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()
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 += 1
l_min.append([k for k, v in d.items() if v == i])
print(l_min)