use*_*551 -2 python list python-2.7 map-function
这是我在编辑器上编辑并在 shell 上编译的代码。
如果我输入整数19,当我打印出c时,它仍然['1','9']不是[1,9]我想要的。我在交互式解释器上尝试了这个,而不是编译 python 文件,它起作用了。
a = raw_input("Please enter a positive number ")
c = []
c = list(a)
map(int, c)
Run Code Online (Sandbox Code Playgroud)
您需要将map输出重新分配给,c因为它不在适当的位置
>>> a=raw_input("Please enter a positive number ")
Please enter a positive number 19
>>> c = list(a)
>>> c = map(int,c) # use the list() function if you are using Py3
>>> c
[1, 9]
Run Code Online (Sandbox Code Playgroud)
请参阅文档map
将函数应用于可迭代的每个项目并返回结果列表。
(强调我的)