简体   繁体   English

随机数生成器,不可散列类型“列表”

[英]Random number generator, unhashable type 'list'

The goal of my code below is to take 10 number from random.我下面代码的目标是从随机数中抽取 10 个数字。 Then print the most data that often appears (mode/modus).然后打印最常出现的数据(模式/方式)。

Here's what i've tried这是我尝试过的

import random
import statistics
from time import sleep

i=0
a=0

var1=input ("min random : ")
var2=input ("max random : ")
bb=int(var1)
ba=int(var2)
data = [[]for z in range(10)]
while i<10:
    rundum=random.randint(bb,ba)
    print(rundum)
    data[a].append(rundum)
    sleep(0.2)
    i=i+1
    a=a+1
tuple(data)
statistics.mode(data)
print(data)

and here's the result这是结果

min random : 1
max random : 10
2
5
9
1
10
7
1
6
9
9
[[2], [5], [9], [1], [10], [7], [1], [6], [9], [9]]
Traceback (most recent call last):
  File "C:\Users\Ez\Documents\belajar random.py", line 20, in <module>
    statistics.mode(data)
  File "C:\Program Files (x86)\Thonny\lib\statistics.py", line 501, in mode
    table = _counts(data)
  File "C:\Program Files (x86)\Thonny\lib\statistics.py", line 252, in _counts
    table = collections.Counter(iter(data)).most_common()
  File "C:\Program Files (x86)\Thonny\lib\collections\__init__.py", line 568, in __init__
    self.update(*args, **kwds)
  File "C:\Program Files (x86)\Thonny\lib\collections\__init__.py", line 655, in update
    _count_elements(self, iterable)
TypeError: unhashable type: 'list'
>>> 

I don't know how to fix this problem Thanks in advance.我不知道如何解决这个问题提前谢谢。

You can use this:你可以使用这个:

import random
import statistics
from time import sleep

bb = int(input("min random : "))
ba = int(input("max random : "))

data = []
for i in range(10):
    r = random.randint(bb, ba)
    data.append(r)
    sleep(0.2)

mode = statistics.mode(data)
print(data)
print("Mode: ", mode)

Sample Result:样本结果:

[9, 7, 8, 4, 5, 8, 2, 3, 10, 6]
Mode:  8

Try this code and to declare an empty list assign data = [] and whenever you need to add a data to the list you can use insert or append methods.试试这个代码并声明一个空列表 assign data = []并且每当您需要将数据添加到列表中时,您可以使用insertappend方法。

import random
import statistics
from time import sleep

i=0
a=0

var1=input ("min random : ")
var2=input ("max random : ")
bb=int(var1)
ba=int(var2)
data = []
while i<10:
    rundum=random.randint(bb,ba)
    print(rundum)
    data.append(rundum)
    sleep(0.2)
    i=i+1
    a=a+1

print("Mode of given data set is {}".format(statistics.mode(data)))

PS: Select the min and max range small otherwise you will end up with, PS:Select 最小和最大范围很小,否则你最终会得到,

StatisticsError: no unique mode ,found x equally common values

were x denotes equal number of random numbers appeared x 表示出现相同数量的随机数

I suggest increase the number of random numbers if possible.如果可能,我建议增加随机数的数量。

OR或者

Rise an exception so that you get an appropriate output like No unique mode in the given list .引发异常,以便您获得适当的 output ,例如No unique mode in the given list

Why are you declare data like that?你为什么要这样声明data It should be just data = [] and then in the for loop, you just need to append to data:它应该只是data = []然后在for循环中,您只需要 append 到数据:

i=0
a=0
import random
from time import sleep
import statistics
var1=input ("min random : ")
var2=input ("max random : ")
bb=int(var1)
ba=int(var2)
data = []
while i<10:
    rundum=random.randint(bb,ba)
    print(rundum)
    data.append(rundum)
    sleep(0.2)
    i=i+1
    a=a+1

data = tuple(data)
print(data)
print(statistics.mode(data))
print(data)

Result:结果:

(10, 7, 6, 3, 5, 5, 4, 1, 6, 5)
5

Or you could shorten your code:或者你可以缩短你的代码:

import random
from time import sleep
import statistics

min_num = input("min random : ")
max_num = input ("max random : ")

for i in range(10):
    data.append(random.randint(min_num, max_num))
    sleep(0.2)

result = statistics.mode(data)
print(data)
print("Result: ", result)
#[10, 7, 6, 3, 5, 5, 4, 1, 6, 5]
#5

This will do the trick.这会成功的。 Each time through the for loop, it will generate a new random number.每次通过for循环,都会产生一个新的随机数。

from random import randint
from statistics import mode

var1 = int(input("min random : "))
var2 = int(input("max random : "))
rand_list = []

for i in range(10):
    rand_list.append(randint(var1, var2))

print(rand_list)
print(f"Most common number is {mode(rand_list)}")

The problem is that you've created a list of lists ([[2], [5], [9], [1], [10], [7], [1], [6], [9], [9]]), rather than a list.问题是您已经创建了一个列表列表([[2]、[5]、[9]、[1]、[10]、[7]、[1]、[6]、[9]、 [9]]),而不是一个列表。 Calling mode on a list of lists will not produce the result you want.在列表列表上调用模式不会产生您想要的结果。

Also, you are not using pythonic constructs.此外,您没有使用 pythonic 构造。 You don't need to preallocate an array and you should iterate over a range rather than incrementing variables in a while loop.您不需要预先分配数组,并且应该在一个范围内迭代,而不是在 while 循环中增加变量。

import random
from time import sleep
import statistics

var1=input ("min random : ")
var2=input ("max random : ")
bb=int(var1)
ba=int(var2)

data = []
for _ in range(10):
    rundum = random.randint(bb,ba)
    data.append(rundum)
    sleep(0.2)
print(statistics.mode(data))
print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM