求助python,map filter
map,filter方法应该会返回一个列表。
但是在我这里,它们总是返回:
<map object at 0x0000000002A2C978>
<filter object at 0x0000000002A2C9E8>
我用的python3.1, 这个也是版本的变动导致的吗?
def map_func(lis):
return lis + 1
li = [1,2,3,4,5]
map_l = map(map_func, li) #将li中所有的数都+1
print (map_l)
就是这个小程序,怎么加print?
看看这个
What's New In Python 3.0 http://blog.csdn.net/t_huanghai/archive/2008/12/04/3443782.aspx
里面有对python3.0 map()函数介绍,参考下 ,试试 print list(map_l)
map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).
What's New In Python 3.0
invalid syntax
看来,应该照它说的, 用for loop
谢谢小康
print (list(map_l))
correct format. mark it.
相关问答:
python和pyqt以及Eric4都已经配置好了,打开Eric4写个python程序比如简单的
print“hello,world”,怎么编译运行了,Start菜单的所有命令都出现和图中差不多的对话框,应当怎么运行了?
求大家指教
没有人用 ......
大家好,我是一个新手,刚开始学python,但是刚开始的helloworld都没法打印,让我很无奈。
我的python安装路径为f:\python31。在path中也设置对了,在windows下运行是这样显示的,希望各位前辈指点一下。谢谢
Pyth ......
以下代码是Tk做了一个简单的密码验证功能,输入一个字符串,并验证其是否正确,请先看代码:
Python code:
from tkinter import *
root = Tk()
e = StringVar()
t = 'cashlu'
def yanzheng():
if e ......
Python code:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def paint(self, factory):
point = factory.getPoint()
......