简体   繁体   English

对带有列表的变量使用算术运算符

[英]Using arithmetic operators with a variable holding a list

I am quite confused on this, so I hope someone can help me. 我对此很困惑,所以我希望有人能帮助我。

So what I have is basically a list in Python, then I have set a variable which within I have used the list name and called the first element such as myVariable = myList[0] , now basically what I want to do is to add an integer value to myVariable to make it so that I get the next value in the list myList[1] and subtract the list as a whole by one each time. 所以我基本上是在Python中创建一个列表,然后设置了一个变量,在其中使用了列表名称并调用了第一个元素,例如myVariable = myList[0] ,现在基本上我想做的就是添加一个使其成为myVariable整数值,以便获得列表myList[1]的下一个值,并myList[1]将列表整体减去一。 I have 8 different elements/items in the list so far. 到目前为止,我在列表中有8个不同的元素/项目。

I keep getting an error usually something along the lines that I can't add an integer (the number value) to the myVariable as it sees it as a string, and I'm unsure on how to get the list to move one result forward and once that's done then subtract the list by one result as it is intended to be removed each time it loops. 我经常收到错误消息,因为我无法将整数(数字值)添加到myVariable因为它将其视为字符串,而且我不确定如何获取列表以将一个结果向前移动然后将列表减去一个结果,因为每次循环时都将其删除。

Example: 例:

def testFunction(testValue):
    myVariable = myList[0]
    testResult (myVariable + testValue) - 1
    print testResult

The testValue being an integer, I need to get the value from myList depending on the integer value of testValue (which it's being added with), so if the testValue was like 3 it would add that to the default myVariable value which is currently 0, then after that result from the list and continue doing this until there is only one result left in this list, which currently consists of 8 elements. testValue是一个整数,我需要从获得的价值myList视的整数值testValue (其中它被添加了),所以如果testValue是一样3,将它添加到默认myVariable值目前0,然后从列表中获取该结果,然后继续执行此操作,直到此列表中仅剩一个结果,该结果当前由8个元素组成。

UPDATE: 更新:

Sample list: ["KaPow", "Boom", "Pow", "Shapow", "Crash", "Whoosh", "Bang", "Pew"] I enter a value into a function such as 3, function below: bam(bamtypes, choice): I enter the suggested value of 3 as "choice" and the bamtypes is just the given list above, then I should receive back the value Shapow. 示例列表:[“ KaPow”,“ Boom”,“ Pow”,“ Shapow”,“ Crash”,“ Whoosh”,“ Bang”,“ Pew”]我在3之类的函数中输入值,以下函数: bam(bamtypes,choice):输入3的建议值作为“ choice”,并且bamtypes只是上面给定的列表,那么我应该收到Shapow的值。 Then what I want to do is basically add myVariable to testValue (I referred to the integer of "choice" in the function when referring to testValue, so in this case it'll be 3). 然后,我要做的基本上是将myVariable添加到testValue(在引用testValue时,我在函数中引用的是“ choice”的整数,因此在这种情况下为3)。 I also referred to bamtypes when saying myList. 我说myList时也提到了bamtypes。 As myVariable basically set to tell the function to begin from the first element in the list such as myVariable = bamtypes[0], but then I want the 3 value from "choices" to be added to the myVariable, so that it becomes bamtypes[3] then I can use .pop to extract that value, then does it again so it is bamtypes[6] and so on until the list only leaves one result. 由于myVariable基本上设置为告诉函数从列表中的第一个元素开始,例如myVariable = bamtypes [0],但随后我希望将“ choices”中的3值添加到myVariable中,从而使其成为bamtypes [ 3]然后,我可以使用.pop提取该值,然后再次进行处理,使其成为bamtypes [6],依此类推,直到列表仅留下一个结果。 Also note as the list has 8 elements, what I want to do is once the 3 adds to the bamtypes[6] it should give me the value of [0] again as it resets, counting twice to the 8th element then returns once more to 0, using up the value 3 for that given retrieval. 还要注意,由于列表中有8个元素,所以我想做的是将3添加到bamtypes [6]中,它应该在重置时再次为我提供[0]的值,对第8个元素计数两次,然后再返回一次为0,则将给定检索结果的值用完3。

  1. When you are doing an arithmetic operation, you can either store the result in a variable or ignore the result. 在执行算术运算时,可以将结果存储在变量中,也可以忽略结果。 If we want to store in a variable, we need to use assignment operator, like this 如果要存储在变量中,则需要使用赋值运算符,如下所示

     testResult = (myVariable + testValue) - 1 # Note the `=` symbol 
  2. The error you get means that the data stored in myVariable is not a number but a character string. 您得到的错误意味着myVariable存储的数据不是数字,而是字符串。 So, you should convert that to an integer like this 因此,您应该将其转换为这样的整数

     testResult = (int(myVariable) + testValue) - 1 # Note the `int` function 

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

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