python随机int附加列表并计数“ x&quot”发生的最高数字和发生的次数
Python的新手,因此所有帮助都非常感谢!我确实检查了一下,发现了几个计数帖子,但找不到任何从列表中打印出最高事件的内容。 (IE 3发生了6倍,发生了4倍,发生了4倍,发生了3倍)
目标:我想随机将代码打印1000数字0,1000,然后能够选择显示多少。
因此,例如num = [0,5,12,5,22,12,0,32,22,0,0,5]我希望能够看到前3个重复的数字,以及该数字发生了多少次。 0-4times,5-3times,12-2times。
代码进程#VALID尝试进行
打印1000倍随机
import random
for x in range(1001):
print(random.randint(0,1001))
将randint直接附加到num
import random
num = []
for x in range(1001):
num.append(random.randint(0,1001))
print(num)
包含提示中,以获取您想要看到多少整数。
import random
num = []
for x in range(1001):
num.append(random.randint(0,1001))
highscore = input("Please enter howmany numbers you'd like to see: ")
print("The top", highscore, "repeated numbers are: ", num)
剩下的问题:如何打印num的HighScore计数(此部分0-4times,5-3times,12-2times。)
尝试在计数问题上(每次打印0。 “ Y”在列表中)
import random
#creates list
num = []
for x in range(0,10):
num.append(random.randint(0,10))
highscore = input("input number of reoccurrence's you want to see: ")
y = num.count(highscore)
print(num, y)
New to python so all help is appreciated! I did check and found several count post but couldn't find any that print the top occurrences from the list. (ie 3 occurred 6x, 4 occurred 4x and 2 occurred 3x)
Goal: I'd like to have the code print 1000 numbers 0,1000 randomly and then be able to choose how many to show.
so for example num = [0,5,12,5, 22,12,0 ,32,22,0,0,5] I want to be able to see say the top 3 repeated numbers and how many times that number occurred. 0-4times, 5-3times, 12-2times.
code Progression #valid attempts are made
Prints 1000 times randomly
import random
for x in range(1001):
print(random.randint(0,1001))
Append the randint directly to num
import random
num = []
for x in range(1001):
num.append(random.randint(0,1001))
print(num)
Includes prompt to get how many integers you want to see.
import random
num = []
for x in range(1001):
num.append(random.randint(0,1001))
highscore = input("Please enter howmany numbers you'd like to see: ")
print("The top", highscore, "repeated numbers are: ", num)
Issues left: how to print highscore's count for num (this part 0-4times, 5-3times, 12-2times.)
Attempt at count issue (prints 0 every time. added num to print to confirm if "y" was in the list)
import random
#creates list
num = []
for x in range(0,10):
num.append(random.randint(0,10))
highscore = input("input number of reoccurrence's you want to see: ")
y = num.count(highscore)
print(num, y)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
most_common
从counter
class 集合中的类库中的类。 documentation> documentation输出
output 最常见的和重复的6次,然后是600是第二常见的,并且重复5次,依此类推。
You can use
most_common
method fromCounter
class incollections
library. documentationoutput:
This output means the number 131 is the most common and repeated 6 times, then 600 is the second most common and it is repeated 5 times and so on.
这是由于无效的类型。 然后尝试
工作正常,
您想查看的重新出现的输入数:4 [5,4,4,0,6,0,2,7,7,9,3,1] 1] 1
It is due to invalid type. Try
Then it works fine,
input number of reoccurrence's you want to see: 4 [5, 4, 0, 6, 0, 2, 7, 9, 3, 1] 1