python 地图功能不起作用

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)

Bha*_*Rao 5

您需要将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

将函数应用于可迭代的每个项目并返回结果列表

(强调我的)

  • 这只能在 Python 2.7.X 中运行。在 Python 3.X 中,您必须使用“list(map(...))”,因为 Python 3.X 中的“map”返回映射对象,而不是直接返回列表。 (2认同)