繁体   English   中英

python map功能不起作用

[英]python map function does not work

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

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

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

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

(强调我的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM