增加一个列表元素中的价值 [英] Increment the value inside a list element

查看:87
本文介绍了增加一个列表元素中的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的那些一个日子,一切我看是刚刚搞乱了。

我有一个模具类(如骰子),我想他们卷100,并计算每个号码的出现。再说了,出100骰子,40是数字'6'。

问题是,如果我硬codeA如果face_value == 1类型的解决方案 - 如果有人改变了面孔对模具的金额 - 我有一个有点问题 - 因为它不会算它们。

我认为占这将是使一个列表(其尺寸为可能的模具面相同),并递增每一次出现在列表内的号码的最佳方式。

总之,我怎么能增加值每个列表元素中?

 diceList = [模具()有效范围内的Q(100)] 


进口随机模具类:
    face_value = 1
    大小= 6    高清的getSize(个体经营):
        回报self.size    高清getFaceValue(个体经营):
        回报self.face_value    高清辊(个体经营):
        self.face_value = random.randint(1,self.size)


高清rollList(N):
    I = 0    A = N [0] .getSize()
    后才可以= [0] *一        对模具在N:
            N [I] .roll()
            X = N [I] .getFaceValue()
            打印(N [I] .face_value)            #faceList [X-1] = 1 + 1
            #PRINT(后才可以)
            I + = 1


我已经重新加工了一点,我已经得到了得到了答案的一种方式 - 我不知道,如果是,虽然做的特别好的办法


高清rollList(N):
    I = 0
    后才可以= []
    A = N [0] .getSize()
    对模具在N:
        N [I] .roll()
        X = N [I] .getFaceValue()
        faceList.append(X)
        I + = 1    打印(后才可以)    C = 1
    而一个> = C:
        打印(faceList.count(C))
        C + = 1


解决方案

 高清rollList(N):
    A = N [0] .getSize()
    后才可以= [0] *一    在N D:
        滑稽()
        X = d.getFaceValue()
        后才可以[X-1] + = 1

I'm having one of 'those' days, and everything I'm looking at is just messing up.

I have a Die class (as in Dice) and I want to roll 100 of them, and count the occurrences of each number. Say, out of 100 dice, 40 were the number '6'.

The issue is, if I hardcode a if face_value == 1 type of solution - if someone changes the amount of faces on the die - I've got a bit of an issue - as it won't count them.

I thought the best way to account for this would be to make a list (which is the same size as the possible die faces) and increment the number inside the list on each occurrence.

In short, how can I increment the value inside of each list element?

diceList = [ Die() for q in range(100)]

import random

class Die:
    face_value = 1
    size = 6

    def getSize(self):
        return self.size

    def getFaceValue(self):
        return self.face_value

    def roll(self):
        self.face_value = random.randint(1, self.size)

def rollList(n):
    i = 0

    a = n[0].getSize()
    faceList = [0] * a

        for Die in n:
            n[i].roll()
            x = n[i].getFaceValue()
            print(n[i].face_value)

            #faceList[x-1] = 1+1
            #print(faceList)
            i += 1


I've re-worked it a little, and I've got a way of getting the answer - I'm not sure if it's a particularly good way of doing it though.

def rollList(n):
    i = 0
    faceList = []
    a = n[0].getSize()
    for Die in n:
        n[i].roll()
        x = n[i].getFaceValue()
        faceList.append(x)
        i += 1

    print(faceList)

    c = 1
    while a >= c:
        print(faceList.count(c))
        c += 1

解决方案

def rollList(n):
    a = n[0].getSize()
    faceList = [0] * a

    for d in n:
        d.roll()
        x = d.getFaceValue()
        faceList[x-1] += 1

这篇关于增加一个列表元素中的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆