在 python 中使用 map(int,input().split()) 的数组更新问题
Array updating problem for using map(int,input().split()) in python
用于在我使用的数组中获取列表输入
map(int,input().split())
但是对于使用这种输入法我无法修改数组,如果我像 [I] 那样迭代然后它显示
TypeError: 'map' object is not subscriptable
你能告诉我如何解决这个问题吗?
我是一名有竞争力的程序员,但我是 python 的新手,我必须使用 map(int,input().split())
获取输入数组
使用 list(map(int,input().split()))
而不是 map(int,input().split())
将您的输入转换为列表,因为 map
函数 returns 一个无法索引的生成器。
如果您无法使用 list
等其他功能,请使用:
# Input: 1, 2, 3, 4, 5
l = [*map(int, input().split())]
print(l)
# Output:
1 2 3 4 5
用于在我使用的数组中获取列表输入
map(int,input().split())
但是对于使用这种输入法我无法修改数组,如果我像 [I] 那样迭代然后它显示
TypeError: 'map' object is not subscriptable
你能告诉我如何解决这个问题吗? 我是一名有竞争力的程序员,但我是 python 的新手,我必须使用 map(int,input().split())
获取输入数组使用 list(map(int,input().split()))
而不是 map(int,input().split())
将您的输入转换为列表,因为 map
函数 returns 一个无法索引的生成器。
如果您无法使用 list
等其他功能,请使用:
# Input: 1, 2, 3, 4, 5
l = [*map(int, input().split())]
print(l)
# Output:
1 2 3 4 5