def is_sqr(x):
return (x**0.5) % 1 == 0
print filter(is_sqr, range(1, 101))
return (x**0.5) % 1 == 0
print filter(is_sqr, range(1, 101))
2019-11-30
# -*- coding: utf-8 -*-
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3= count()
print(f1(), f2(), f3())
print(count())
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3= count()
print(f1(), f2(), f3())
print(count())
2019-11-28
# -*- coding: utf-8 -*-
from functools import reduce
def calc_prod(lst):
def prod(x, y):
return x*y
return reduce(prod, lst)
f = calc_prod([1, 2, 3, 4])
print(f)
from functools import reduce
def calc_prod(lst):
def prod(x, y):
return x*y
return reduce(prod, lst)
f = calc_prod([1, 2, 3, 4])
print(f)
2019-11-28
只有父类和子类都定义了 __slots__ 属性,子类的属性限制才有效
子类的属性限制等于父类的 __slots__ 属性加子类的 __slots__ 属性
子类的属性限制等于父类的 __slots__ 属性加子类的 __slots__ 属性
2019-11-24
看不懂得童鞋可以看看这个通俗易懂的例子:https://www.jb51.net/article/158814.htm
2019-11-19
def cmp_ignore_case(s1, s2):
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2019-11-18
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2019-11-11
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2019-11-11
def prod(x,y):
return x*y
def reduce_prod(lst):
return reduce(prod,lst)
def calc_prod(lst):
def calc_prod():
return reduce_prod(lst)
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
return x*y
def reduce_prod(lst):
return reduce(prod,lst)
def calc_prod(lst):
def calc_prod():
return reduce_prod(lst)
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2019-11-02
python3 的map 用法不一样了。要实现相同的化。
def format_name(s):
return str.capitalize(s)
print(list(map(format_name,['admin','LISA','barT'])))
def format_name(s):
return str.capitalize(s)
print(list(map(format_name,['admin','LISA','barT'])))
2019-10-31