Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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/1/list/4.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 3.x Python zip()函数不工作_Python 3.x_List_Zip - Fatal编程技术网

Python 3.x Python zip()函数不工作

Python 3.x Python zip()函数不工作,python-3.x,list,zip,Python 3.x,List,Zip,我不熟悉堆栈溢出和Python。 我试图通过索引替换列表中的项来构建word,当我运行此代码时,只有第一个实例是replace word = "DRIPPING" letter = "P" checkList = [" _ "] * len(word) letterLocation=[3,4] for (index, replacement) in zip(letterLocation, letter): checkList[index] = replacement print(ch

我不熟悉堆栈溢出和Python。 我试图通过索引替换列表中的项来构建word,当我运行此代码时,只有第一个实例是replace

word = "DRIPPING"
letter = "P"
checkList = [" _ "] * len(word)
letterLocation=[3,4]

for (index, replacement) in zip(letterLocation, letter):
    checkList[index] = replacement

print(checkList) 
返回


欢迎提供任何帮助。

zip
接受两个或多个iterable,并从每个iterable生成包含一个元素的元组,直到其中一个iterable用完为止

由于
letter
只包含一个字符,
zip
因此只会发出一个单元组:

>>> list(zip(checkList,letter))
[(' _ ', 'P')]
在这里,您不需要
zip
,只需在
检查表上迭代,然后将
字母
分配给所有这些索引:

for index in letterLocation:  # look ma, no zip
    checkList[index] = letter
对于letterLocation中的索引:#查找ma,无邮政编码

检查表[索引]=字母
zip
工作正常,但您在不应使用
zip
的地方使用了
zip
。谢谢威廉。你是个明星!相信我会把事情复杂化。