为什么下面的脚本会报错:
payIntList[i] = payIntList[i] + 1000
TypeError: 'map' object is not subscriptable
payList = []
numElements = 0
while True:
payValue = raw_input("Enter the pay amount: ")
numElements = numElements + 1
payList.append(payValue)
choice = raw_input("Do you wish to continue(y/n)?")
if choice == 'n' or choice == 'N':
break
payIntList = map(int,payList)
for i in range(numElements):
payIntList[i] = payIntList[i] + 1000
print payIntList[i]
原文由 station 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 Python 3 中,
map
返回类型为map
的可迭代对象,而不是可订阅列表,它允许您编写map[i]
要强制列表结果,请写但是,在许多情况下,您可以通过不使用索引来更好地编写代码。例如,使用 列表理解: