如何使用变量作为标识符在列表中输入项目? 我有一个不错的C++背景,但我对Python是新手。我正在尝试编写一个基本程序,允许用户指定公司的股东人数,然后询问每个股东在三个方面的评分(工作的有用性、工作的重要性、工作的难度)

如何使用变量作为标识符在列表中输入项目? 我有一个不错的C++背景,但我对Python是新手。我正在尝试编写一个基本程序,允许用户指定公司的股东人数,然后询问每个股东在三个方面的评分(工作的有用性、工作的重要性、工作的难度),python,list,Python,List,我想将用户的评分存储在这三个刻度上的某个位置,然后稍后再显示。我仍然不确定对这3种品质中的每一种使用3个列表是否是最有效的方法。无论如何,我在下面的代码中尝试的是分配一个变量userrem作为标识符,使用它添加列表中的新项目 因此,例如,userrem的初始值为0。因此,我的理解是,有用性[userrem]=input()应该将输入的值添加为列表中的第一项。然后,正如您在代码中看到的,while循环继续进行,并且userrem增加1。因此,我认为对于循环的第二次迭代,有用性[userrem]=i

我想将用户的评分存储在这三个刻度上的某个位置,然后稍后再显示。我仍然不确定对这3种品质中的每一种使用3个列表是否是最有效的方法。无论如何,我在下面的代码中尝试的是分配一个变量
userrem
作为标识符,使用它添加列表中的新项目

因此,例如,
userrem
的初始值为0。因此,我的理解是,
有用性[userrem]=input()
应该将输入的值添加为列表中的第一项。然后,正如您在代码中看到的,while循环继续进行,并且
userrem
增加1。因此,我认为对于循环的第二次迭代,
有用性[userrem]=input()
应该将输入的值添加为列表中的第二项

但是,在循环的第一次迭代中将值输入到
有用性[userrem]
之后,我不断得到错误
索引器错误:列表分配索引超出范围

因此,我的问题如下:-

  • 使用列表是最有效的方法吗
  • 实现我想要的目标的另一种方式是什么
  • 每个股东都有一份名单,上面有三个 每个项目都有三种质量,而不是有三个带有 项目数量未知(可能无限!)?但如果我们有一份清单 对于每个股东,名单的数量可能是未知的 可能是无限的,即使每个列表中的项目只有3个。 我如何确定哪种方法最有效
  • 谢谢

    def func_input():  
        userrem=0 # Variable used as the identifier while adding items to the lists
        global user_n # Total number of users, accessed during input
        user_n=0 
        user_n=int(input('How many shareholders are there?'))
        while userrem<user_n:
            usefulness[userrem]=int(input('Rate the usefulness of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
            significance[userrem]=int(input('Rate the significance of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
            difficulty[userrem]=int(input('Rate the difficulty of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
            userrem=userrem+1
    
    def func_input():
    userrem=0#在向列表添加项目时用作标识符的变量
    全局用户_n#输入期间访问的用户总数
    用户\u n=0
    user_n=int(输入('有多少股东?'))
    
    而userrem最简单的方法是将列表更改为字典:

    usefulness = {}
    significance = {}
    difficulty = {}
    
    它使用与访问列表相同的语法,并允许分配到以前未分配的索引

    如果您希望继续使用列表,则需要输入另一个变量,然后将
    追加到列表中,或者提前创建所需大小的列表

    您可以将这3个值组合成一个元组或列表,并将其存储在单个列表/字典中,而不是有3个列表或字典。下面是一些用于演示在列表中存储元组的代码:

    scores = [None]*user_n
    for userrem in range(user_n):
        usefulness = int(input('Rate the usefulness of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
        significance = int(input('Rate the significance of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
        difficulty = int(input('Rate the difficulty of the work performed by shareholder# '+str(userrem+1)+' [Range=0-5]'))
        scores[userrem] = (usefulness, significance, difficulty)