我正在尝试编写一个函数,它将函数应用于列表。我想把单子上所有的字都大写,但没法用以下是我迄今为止所做的:
list = ("hello", "this", "is", "a", "test")
def firstFunction(x):
return list.upper()
print firstFunction
我得到的错误是:
<function firstFunction at 0x0000000002352A58>
我真的很纠结于下一步该怎么办,任何帮助都将不胜感激。
编辑:
我刚换了,但还是不行:
mylist = ("hello", "this", "is", "james")
def firstFunction(x):
return may(lambda: x.upper(), mylist)
print firstFunction()
最佳答案
那不是个错误它是函数在内存中的地址你看到它是因为你没有调用这个函数。
总的来说,代码有三个问题:
您没有调用该函数。在它之后添加(...)
就可以了。
您没有向函数传递它所需的参数。
元组上没有upper
方法(在本例中list
是元组)。
下面是一个固定版本的代码,可以满足我的要求:
# Don't name a variable 'list' -- it overshadows the built-in.
lst = ("hello", "this", "is", "a", "test")
def firstFunction(x):
return tuple(y.upper() for y in x)
print firstFunction(lst)
输出:
('HELLO', 'THIS', 'IS', 'A', 'TEST')
这里有一些关于这里所做一切的参考资料:
http://docs.python.org/2/reference/compound_stmts.html#function-definitions
https://wiki.python.org/moin/Generators
http://docs.python.org/2.7/library/stdtypes.html#str.upper