python随机int附加列表并计数“ x&quot”发生的最高数字和发生的次数

发布于 2025-01-21 04:14:14 字数 1331 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2025-01-28 04:14:14

您可以使用most_commoncounter class 集合中的类库中的类。 documentation> documentation

from collections import Counter
import random

number_of_elements = 1000


counter = Counter([random.randint(0,1001) for i in range(number_of_elements)])


# printing 3 most common elements.
print(counter.most_common(3))

输出

[(131, 6), (600, 5), (354, 5)]

output 最常见的和重复的6次,然后是600是第二常见的,并且重复5次,依此类推。

You can use most_common method from Counter class in collections library. documentation

from collections import Counter
import random

number_of_elements = 1000


counter = Counter([random.randint(0,1001) for i in range(number_of_elements)])


# printing 3 most common elements.
print(counter.most_common(3))

output:

[(131, 6), (600, 5), (354, 5)]

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.

明媚如初 2025-01-28 04:14:14

这是由于无效的类型。 然后尝试

y = num.count(int(highscore))

工作正常,

您想查看的重新出现的输入数:4 [5,4,4,0,6,0,2,7,7,9,3,1] 1] 1

It is due to invalid type. Try

y = num.count(int(highscore))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文