map()就像画地图,根据地形按照比例画到图上。每一个地形就对应一个图形。reduce()像贪吃蛇,有初始长度,挨着吃过去。吃到最后就剩一个值了。
2018-06-16
最赞回答 / 微sun
因为你先运行了a=b之后a的值已经变了,所以下面b=a+b实际上是b=2*b,所以你的序列是
[0, 1, 2, 4, 8, 16, 32, 64, 128, 256]这样子的~
2018-06-14
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-06-14
已采纳回答 / 微sun
上面的:def count(): fs = [] for i in range(1, 4): def f(i): return i*i#返回数 fs.append(f(i))#所以fs中的元素是 数值 return fsf1, f2, f3 = count()print f1, f2, f3#fs中的元素是 数值(所以不能用print f1(), f2(), f3())下面的:def count(): fs = [] for ...
2018-06-13
def f(lst):
def k():
def g(i):
return i*i
return map(g,lst)
return k
m = f([1,2,3])
print(list(m()))
def k():
def g(i):
return i*i
return map(g,lst)
return k
m = f([1,2,3])
print(list(m()))
2018-06-13
有的同学不是很明白生成顺序
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
fs.append(r)
return fs
for name in count():
print name()
就明白了
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
fs.append(r)
return fs
for name in count():
print name()
就明白了
2018-06-13
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x, y: x * y, lst)
return lazy_prod
pass
def lazy_prod():
return reduce(lambda x, y: x * y, lst)
return lazy_prod
pass
2018-06-12
def f(lst):
def g():
def mul(x, y):
return x*y
return reduce(mul,lst)
return g
lst = [2, 3, 4, 5]
a = f(lst)
print(a())
def g():
def mul(x, y):
return x*y
return reduce(mul,lst)
return g
lst = [2, 3, 4, 5]
a = f(lst)
print(a())
2018-06-12
python3中已经用不了上面的方法了,我用这种方法排序:
>>> my_list = ['bob', 'about', 'Zoo', 'Credit']
>>> sorted(my_list, key=lambda x: x.lower())
['about', 'bob', 'Credit', 'Zoo']
>>> my_list = ['bob', 'about', 'Zoo', 'Credit']
>>> sorted(my_list, key=lambda x: x.lower())
['about', 'bob', 'Credit', 'Zoo']
2018-06-12
最赞回答 / 慕工程0057670
刚刚测试了一下你的代码
Traceback (most recent call last): File "index.py", line 17, in print Person.how_many() TypeError: how_many() takes no arguments (1 given)报错是因为how_many没有获取到参数,类方法的第一个参数将传入类本身(引用), 所以必须要有一个参数作为类, 并不是Person.__count报错
2018-06-11