-2

这是我在编辑器上编辑并在 shell 上编译的代码。
如果我输入整数 19,当我打印出 c 时,它仍然['1','9']不是[1,9]我想要的。我在交互式解释器上尝试了这个,而不是编译 python 文件,它工作正常。

a = raw_input("Please enter a positive number ")    
c = []    
c = list(a)    
map(int, c) 
4

1 回答 1

4

您需要重新分配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]

请参阅上的文档map

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

(强调我的)

于 2015-06-06T22:30:43.303 回答