【问题标题】:Count occurrence´s of Element in numpy array (list)计算 numpy 数组(列表)中元素的出现次数
【发布时间】:2020-10-14 17:30:49
【问题描述】:

我正在编写这个非常简单的代码来了解 python。我的目标是,当我多次掷两个骰子时,可能会出现一个二项分布图。 为此,我编写了这些代码行:

    import random
    import numpy
    
    class Dice:
        def roll(self):
            first = random.randint(1, 6)
            return first


    class Dice2:   
        def roll(self):
            second = random.randint(1, 6)
            return second
   
   
    storage1 = []
    storage2 = []
    for rolling in range(10, 0, -1):
        dice = Dice()
        storage1.append(dice.roll())
        storage2.append(dice.roll())
    
    list1 = numpy.array((storage1)) + numpy.array((storage2))
    
    print(list1)
    
    
    x = 5
    count = 0
    
    for dice in list1:
        if(dice == x):
            count = count + 1
        print(count1)

所以我在这里要做的是输出一个元素的计数,在这种情况下 x = 5,换句话说,当我掷 2 个骰子时,我会扔多少次 5。 尤其是最后一部分:

 list1 = numpy.array((storage1)) + numpy.array((storage2))
    
    print(list1)
    
    
    x = 5
    count = 0
    
    for dice in list1:
        if(dice == x):
            count = count + 1
        print(count1)

好像不行,输出是我看不懂的,输出是这样的:

[ 2  7  7  8  7  4  7  9 10 10] 
  
#This is the output from the line: list1 = numpy.array((storage1)) + numpy.array((storage2))
# so this part I understand, it is the addition of dice1 and dice2, like wished

1
1
1
1
1
1
1
1
1
1

# This is the Output from the loop and finally the print(count1)

我想知道,我如何存储出现的次数,从 2 到 12 的任何数字(来自滚动两个骰子)都会出现。

【问题讨论】:

    标签: python arrays numpy for-loop counting


    【解决方案1】:

    对代码进行简单修改以获取滚动值计数。如果您特别想使用 Numpy,请告诉我。

    代码

    from random import randint  # only using randint so only import it
    import numpy as np          # not needed
    
    class Dice:
        def roll(self):
            return randint(1, 6)
    
    frequencies = [0]*13        # array to hold results of dice roll 
                                # will use frequencies 2 to 12)
    
    dice = Dice()
    remaining_rolls = 10000     # number of remaining rolls 
    while remaining_rolls > 0:
        roll = dice.roll() + dice.roll() # roll sum
        frequencies[roll] += 1   # increment array at index roll by 1
        remaining_rolls -= 1
        
    print(frequencies)
    

    输出

    [0, 0, 272, 583, 829, 1106, 1401, 1617, 1391, 1123, 863, 553, 262]
    

    使用列表理解替代 while 循环

    frequencies = [0]*13 
    for roll in [dice.roll() + dice.roll() for _ in range(10000)]:
        frequencies[roll] += 1
    print(frequencies) # Simpler Output
    

    说明

    作业:

    frequencies = [0]*13
    

    创建一个包含 13 个元素的数组,索引从 0 到 12,最初用零填充。

    每个骰子的总和是一个介于 2 到 12 之间的数字(即 11 个值)

    为了增加我们使用的滚动计数:

     frequencies[roll] += 1
    

    这是syntactic sugar,用于:

    frequencies[roll] = frequencies[roll] + 1
    

    例如,如果 roll = 5,我们将频率加 1[5] 这会增加滚动次数为 5 的计数。

    两个箱子总是零:

    frequencies[0] and frequencies[1]
    

    这是因为 2

    【讨论】:

    • 很好,非常简单和优雅,谢谢.. 还有一些问题:为什么输出中有 2 个零?索引滚动 1 处的增量数组如何工作?换句话说,频率[roll] += 1 中正在发生什么样的过程?
    • @BenjaminDiLorenzo--嗨本杰明。我添加了一些解释。这能回答你的问题吗?
    • 是的,主要是这样。我只是不知道通过>>>频率= [0] * 13暗示了什么样的进程/操作员?这会创建一个具有给定索引的列表吗?在哪里可以找到有关此语法的更多信息?
    • @BenjaminDiLorenzo--this shows 创建长度为 n 且具有重复值的列表的三种方法。在我们的例子中,长度为 13,值为 0,我们使用的是第三种方法。
    猜你喜欢
    • 2020-06-09
    • 2013-04-07
    • 2011-08-10
    • 2022-11-30
    • 1970-01-01
    • 2015-08-22
    • 2018-11-20
    • 2012-08-03
    相关资源
    最近更新 更多