鱼C论坛

 找回密码
 立即注册
查看: 1252|回复: 9

[已解决]我想统计一个数字出现的次数

[复制链接]
发表于 2019-8-14 17:32:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我做出一个随机的5个数为一组,然后让循环100次,最后要统计各个数字出现的次数。
大神们这个命令要怎么写
最佳答案
2019-8-14 17:56:25
  1. import random
  2. jishu = 0
  3. rand = 0
  4. one = 0
  5. two = 0
  6. three = 0
  7. four = 0
  8. five = 0
  9. while jishu < 100:
  10.     jishu = jishu + 1
  11.     for i in range(5):
  12.         rand = random.randint(1,5)
  13.         if rand == 1:
  14.             one = one + 1
  15.         elif rand == 2:
  16.             two = two +1
  17.         elif rand == 3:
  18.             three = three + 1
  19.         elif rand == 4:
  20.             four = four +1
  21.         elif rand == 5:
  22.             five = five + 1

  23. print("1出现了", one, "次")
  24. print("2出现了", two, "次")
  25. print("3出现了", three, "次")
  26. print("4出现了", four, "次")
  27. print("5出现了", five, "次")
复制代码

有问题再问哦
大佬别打我
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 17:56:25 | 显示全部楼层    本楼为最佳答案   
  1. import random
  2. jishu = 0
  3. rand = 0
  4. one = 0
  5. two = 0
  6. three = 0
  7. four = 0
  8. five = 0
  9. while jishu < 100:
  10.     jishu = jishu + 1
  11.     for i in range(5):
  12.         rand = random.randint(1,5)
  13.         if rand == 1:
  14.             one = one + 1
  15.         elif rand == 2:
  16.             two = two +1
  17.         elif rand == 3:
  18.             three = three + 1
  19.         elif rand == 4:
  20.             four = four +1
  21.         elif rand == 5:
  22.             five = five + 1

  23. print("1出现了", one, "次")
  24. print("2出现了", two, "次")
  25. print("3出现了", three, "次")
  26. print("4出现了", four, "次")
  27. print("5出现了", five, "次")
复制代码

有问题再问哦
大佬别打我
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-14 17:57:11 | 显示全部楼层
count 就是你想要的出现次数的列表~~~

  1. import random

  2. random_number = []

  3. for i in range(100):
  4.     for j in range(5):
  5.         random_number.append(random.randint(1, 100))

  6. count = {}
  7. for i in range(1, 101):
  8.     count[i] = random_number.count(i)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-14 18:11:37 | 显示全部楼层
什么意思???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-14 18:22:25 | 显示全部楼层
先谢谢大神了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-14 18:37:26 | 显示全部楼层
Thanks&#9834;(&#65381;ω&#65381;)&#65417;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-14 19:52:19 | 显示全部楼层
i = 0
while i < 100:
    import random
    a = 0
    reward = []

    for n in range(1,10):
        if len(reward) <= 4:
        
            n = random.randint(1,10)
            if n in reward: continue
            reward.append(n)
            a += 1
        
    print(reward)
    i += 1
print('程序结束')


这是我写的,就是不用把里面出现的数字统计,比如1出现了多少次,2出现了多少次。。。。。这样
刚才可能我表达不明确抱歉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-14 19:55:48 | 显示全部楼层
Jenson980 发表于 2019-8-14 17:56
有问题再问哦
大佬别打我


i = 0
while i < 100:
    import random
    a = 0
    reward = []

    for n in range(1,10):
        if len(reward) <= 4:
        
            n = random.randint(1,10)
            if n in reward: continue
            reward.append(n)
            a += 1
        
    print(reward)
    i += 1
print('程序结束')


这是我写的,就是不用把里面出现的数字统计,比如1出现了多少次,2出现了多少次。。。。。这样
刚才可能我表达不明确抱歉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-14 20:13:14 | 显示全部楼层
gaoyuelong 发表于 2019-8-14 19:55
i = 0
while i < 100:
    import random

你这个import random, a = 0, reward = []都要放外面,还有你这if len(reward) <=4干什么的,我不懂,我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-14 20:18:59 | 显示全部楼层
gaoyuelong 发表于 2019-8-14 19:55
i = 0
while i < 100:
    import random
  1. i = 0
  2. import random
  3. a = 0
  4. reward = []
  5. while i < 100:
  6.     for n in range(1,10):
  7.         n = random.randint(1,10)
  8.         if n in reward: continue
  9.         reward.append(n)
  10.         a += 1
  11.         
  12.     print(reward)
  13.     i += 1
  14. print("出现的数字有:", end="")
  15. for i in range(0,10):
  16.     print(reward[i], end=",")
  17. print('程序结束')
复制代码

你的意思是这样吗,试试看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-11 03:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表