Python 3.x Python zip()函数不工作
我不熟悉堆栈溢出和Python。 我试图通过索引替换列表中的项来构建word,当我运行此代码时,只有第一个实例是replacePython 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
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
。谢谢威廉。你是个明星!相信我会把事情复杂化。