简体   繁体   English

如何从列表中随机选择一个变量,然后在python中修改它?

[英]How do I randomly select a variable from a list, and then modify it in python?

Here's my python 3 code. 这是我的python 3代码。 I would like to randomly select one of the cell variables (c1 through c9) and change its value to the be the same as the cpuletter variable. 我想随机选择一个单元格变量(c1到c9)并将其值更改为与cpuletter变量相同。

import random

#Cell variables
c1 = "1"
c2 = "2"
c3 = "3"
c4 = "4"
c5 = "5"
c6 = "6"
c7 = "7"
c8 = "8"
c9 = "9"
cells = [c1, c2, c3, c4, c5, c6, c7, c8, c9]

cpuletter = "X"

random.choice(cells) = cpuletter

I'm getting a "Can't assign to function call" error on the "random.choice(cells)." 我在“random.choice(cells)”上收到“无法分配函数调用”错误。 I assume I'm just using it incorrectly? 我假设我只是错误地使用它? I know you can use the random choice for changing a variable like below: 我知道您可以使用随机选择更改变量,如下所示:

import random
options = ["option1", "option2"]
choice = random.choice(options)

Problem: 问题:

random.choice(cells) returns a random value from your list, for example "3" , and you are trying to assign something to it, like: random.choice(cells)从列表中返回一个随机值,例如"3" ,并且您正在尝试为其分配内容,例如:

"3" = "X"

which is wrong. 这是错的。

Instead of this, you can modify the list , for example: 而不是这样,您可以修改list ,例如:

cells[5] = "X"

Solution: 解:

You can use random.randrange() . 您可以使用random.randrange()

import random
cells = [str(i) for i in range(1,10)] # your list
cpuletter = 'X'

print(cells)
random_index = random.randrange(len(cells)) # returns an integer between [0,9]
cells[random_index] = cpuletter
print(cells)

Output: 输出:

['1', '2', '3', '4', '5', '6', '7', '8', '9']
['1', '2', '3', '4', '5', '6', '7', 'X', '9']

Random.choice(cells) returns a random element from cells , so if it returned element #0 and element #0 was the value "1" , your original statement would essentially be saying "1" = "X" - obviously you can't assign the string literal to be something else, it's not a variable. Random.choice(cells)cells Random.choice(cells)返回一个随机元素 ,所以如果它返回元素#0而元素#0是值"1" ,那么你的原始语句基本上会说"1" = "X" - 显然你可以' t将字符串文字指定为其他内容,它不是变量。

You'd instead want to get a random element # , and assign cells[rand_elt_num] . 你想要得到一个随机元素# ,然后分配cells[rand_elt_num] You could get the random element number by something simply like: 您可以通过以下内容获取随机元素编号:

rand_elt_num = random.randint(0, len(cells)-1 )  #get index to a random element
cells[rand_elt_num] = "X"    # assign that random element

I think this is the same as what the other answer says, they just used random.randrange() instead. 我认为这与其他答案所说的相同,他们只是使用random.randrange()代替。

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

相关问题 我可以从列表中随机选择……但是如何将其用作不同命令的变量? 蟒蛇 - I can select randomly from a list… but how do I use this as a variable for different commands? Python 如何从列表中随机 select 一个变量,然后将其重新分配给另一个变量? - How do I randomly select a variable from a list, and then re assign it to another variable? 如何从列表中随机选择一个项目 - How do I randomly select an item from a list 如何更新使用 python 从列表中随机提取的变量? - How do I update a variable that is pulling randomly from a list using python? Python:如何从字典键中随机选择一个值? - Python: How do I randomly select a value from a dictionary key? 我如何从输入中随机选择一个单词(python) - how do i randomly select a word from an input (python) 如何从列表中随机生成 python select 某些内容,如果在输入提示中键入,它将按预期显示确切答案 - How do I make python randomly select something from a list and if typed in the input prompt, it will show the exact answer as intended (python) 如何修改列表? - (python) How do I modify a list? 如何从python列表中随机选择一对相邻元素 - How to select randomly a pairs of adjacent elements from a python list 如何随机 select 列表中的项目? - How can I randomly select an item from a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM