已采納回答 / 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