Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替换列表中的值_Python - Fatal编程技术网

Python 替换列表中的值

Python 替换列表中的值,python,Python,基本上,我正在尝试创建一个程序,在索引列表中有1的地方,用1到6之间的随机整数替换diceList的值。这就是我到目前为止所做的: import random def replaceValues(diceList, indexList): newList = diceList for i in indexList: if indexList == 1: newList[i] = random.randint(1,6) ret

基本上,我正在尝试创建一个程序,在索引列表中有1的地方,用1到6之间的随机整数替换diceList的值。这就是我到目前为止所做的:

import random
def replaceValues(diceList, indexList):
    newList = diceList
    for i in indexList:
        if indexList == 1:
            newList[i] = random.randint(1,6)
        return newList

我正在执行
replaceValues([1,2,3,4,5],[0,1,0,1,0])
,我应该得到
[1,x,3,x,5]
,其中x应该是1到6之间的随机数。问题是它当前返回[1,2,3,4,5]

索引列表
a
列表
?如果是,那么
列表==1
意味着什么

if indexList == 1:
    newList[i] = random.randint(1,6)
你的意思可能是
i==1

编辑:

我想你在找这样的东西:

def replaceValues(diceList, indexList):
    newList = diceList
    if len(diceList) == len(indexList):
        for pos in range(0, len(indexList)):
            if indexList[pos] == 1:
                newList[pos] = random.randint(0,6)
        return newList

你的意思是2和6之间的
,对吗?…安那有什么问题吗?不,我的意思是1和6,但我看到了我犯的错误,现在它只返回列表[1,2,3,4,5]是索引列表是一个0和1的列表。如果有一个“1”,那么它应该在“1”处用一个随机数替换骰子列表。对不起,我现在更了解你想要什么了。更新了我的帖子。谢谢贾斯汀!成功了!令人惊叹的!快乐编码:D