python映射函数执行n

2024-04-26 19:00:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: 文件代码编辑numbermapinputraw整数
1条回答
网友
1楼 · 发布于 2024-04-26 19:00:14

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

参见docs on ^{}

Apply function to every item of iterable and return a list of the results.

(重点是我的)

相关问题 更多 >