python map功能不起作用 [英] python map function does not work

查看:79
本文介绍了python map功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the code I edit on an editor and compile on a shell.
If I enter the integer 19, when I print out c, it is still ['1','9'] instead of the [1,9] I want. I tried this on the interactive interpreter instead of compiling a python file and it worked.

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

推荐答案

您需要将map输出重新分配给c,因为它不就位

You need to reassign the map output to c as it is not in-place

>>> 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

对可迭代的每个项目应用功能,并 返回结果列表 .

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

(重点是我的)

这篇关于python map功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆