已采纳回答 / qq_嗑瓜子虫_03382717
这里你可以把它当作函数返回值, 可以理解为: 有一个myabs 指向的是一个lambda函数的地址, 这个函数以x为参数, 然后这句:
-x if x < 0 else x可以把这段代码看成是
y = -x if x < 0 else x return y这样是不是就明白了?,如果上面的y右边的表达式不是很明白可以去看一下if...else...的语法.
2020-06-24
最赞回答 / qq_嗑瓜子虫_03382717
这个问题非常有意思了?首先, 你的两个程序的结果可不仅仅是多出一个空字符的问题,而是function的返回值问题. filter(is_sqr, range(101)) 用于过滤range(101)中不符合条件(is_sqr)的元素,返回一个迭代器对...
2020-06-23
import time
def performance(f):
def t(*args):
t1=time.time()
f(*args)
t2=time.time()
print 'call '+ f.__name__ +'() in',t2-t1
return f(*args)
return t
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def t(*args):
t1=time.time()
f(*args)
t2=time.time()
print 'call '+ f.__name__ +'() in',t2-t1
return f(*args)
return t
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2020-06-21
正确答案
import json
class Students(object):
def __init__(self):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print (json.load(s))
import json
class Students(object):
def __init__(self):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print (json.load(s))
2020-06-21
def calc_prod(lst):
def lazy_prod():
def prod(x,y):
return x*y
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def prod(x,y):
return x*y
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2020-06-21
import math
def is_sqr(x):
t =math.sqrt(x)
if t % (int(t)) ==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
t =math.sqrt(x)
if t % (int(t)) ==0:
return x
print filter(is_sqr, range(1, 101))
2020-06-20
from functools import reduce
def calc_prod(lst):
def quad():
return reduce(ji,lst)
return quad
def ji(x,y):
return x * y
f = calc_prod([1, 2, 3, 4])
print(f())
哎还是学py3吧
def calc_prod(lst):
def quad():
return reduce(ji,lst)
return quad
def ji(x,y):
return x * y
f = calc_prod([1, 2, 3, 4])
print(f())
哎还是学py3吧
2020-06-19
import numpy
def calc_prod(lst):
return numpy.prod(lst)
f = calc_prod([1, 2, 3, 4])
print(f)
def calc_prod(lst):
return numpy.prod(lst)
f = calc_prod([1, 2, 3, 4])
print(f)
2020-06-18
最赞回答 / showmeyourcode
def count(): fs = [] for i in range(1,4): def f(): return i*i fs.append(f()) return fs这样返回的是list
2020-06-16
最新回答 / whistle971225
map(x, list)函数对list对象中每个元素进行函数x变换,这里不需要调用map函数,直接return s1即可得到首字母大写的list
2020-06-12
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2020-06-09
最赞回答 / 慕运维5384406
回来从头复习了一下,即使是自定义函数也可用在functools.partial中,楼主只是 functools.partial(sorted, cmp_ignore_case)这里没有加上cmp,具体可看下面。import functoolsdef cmp_ignore_case(s1, s2): if s1.upper()>s2.upper(): return 1 if s1.upper()<s2.upper(): return -1 else:...
2020-06-09
def __cmp__(self, s):
if self.score<s.score:
return 1
if self.score>s.score:
return -1
if self.score==s.score:
if self.name>s.name:
return 1
if self.name<s.name:
return -1
return 0
if self.score<s.score:
return 1
if self.score>s.score:
return -1
if self.score==s.score:
if self.name>s.name:
return 1
if self.name<s.name:
return -1
return 0
2020-06-06