已采纳回答 / 重舟
Python3不再支持iteritems,要使用kw.items()&s P&rso&(ob class Person(object): def __init__(self, name, gender, birth, **kw): self.name = name self.gender = gender self.birth = birth for k, v in kw.items(): setattr(self, k, v) &min& = &ers xi...
2018-03-23
def count():
fs = []
for i in range(1, 4):
fs.append(lambda m=i : m**2)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
fs.append(lambda m=i : m**2)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-03-23
class Fib(object):
def __call__(self,num):
a, b = 0, 1
for i in range(num):
print(b)
a, b = b, a + b
f = Fib()
f(10)
def __call__(self,num):
a, b = 0, 1
for i in range(num):
print(b)
a, b = b, a + b
f = Fib()
f(10)
2018-03-23
三种方法:
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-23
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda x:x.name)
print L2[0].name
print L2[1].name
print L2[2].name
God,你离成神已经不远了!下一节吧。
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda x:x.name)
print L2[0].name
print L2[1].name
print L2[2].name
God,你离成神已经不远了!下一节吧。
2018-03-22
# -*- coding: UTF-8 -*-
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score>=90:
return "A"
elif self._score>=60 and self._score <90:
return "B"
else:
return "C"
def __init__(self, name, score):
self.name = name
self._score = score
def get_grade(self):
if self._score>=90:
return "A"
elif self._score>=60 and self._score <90:
return "B"
else:
return "C"
2018-03-22
import math
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
a=list(filter(is_sqr, range(1, 101)))
print (a)
def is_sqr(x):
return math.sqrt(x) == int(math.sqrt(x))
a=list(filter(is_sqr, range(1, 101)))
print (a)
2018-03-22
最新回答 / Lucky_lda
sorted_ignore_case 利用Python中的偏函数将其第一个参数固定,仅传入第二个参数,第二个参数传入后先调用upper()函数转换成大写字母,再调用cmp()比较函数,比较二者的顺序,
2018-03-22
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self, name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-03-21
def format_name(s):
return s[:].title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:].title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-21
已采纳回答 / 慕前端3429475
如果被装饰器封装之后,f变量指向的函数已经不是函数f了(2-11有讲),而是装饰器函数,所以直接用f.__name__打出来也不是f了,这一节有例子的。如果想打印出f,就需要对f变量的属性重新赋值,这也就是functools的作用@new_fndef f1(x): return x*2相当于def f1(x): return x*2f1 = new_fn(f1)我记得有一句,f1的原始定义函数被完全隐藏了所以想要保留之前的属性的话就要在被替代前重新赋值给新的函数
2018-03-21
默认升序:L2 = sorted(L1,key=lambda s:s.name)
设置降序:L2 = sorted(L1,key=lambda s:s.name,reverse=True)
设置降序:L2 = sorted(L1,key=lambda s:s.name,reverse=True)
2018-03-21
@cnnlion
sorted([36,5,12,9,21],reversed_cmp)的执行过程:
5 < 36
[5, 36, 12, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
12 < 36
[5, 12, 36, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
9 < 12
[5, 9, 12, 36, 21]
9 > 5
[5, 9, 12, 36, 21]
21 > 12
[5, 9, 12, 21, 36]
21 < 36
[5, 9, 12, 36, 21]
#和排好序的列表中位数比较
sorted([36,5,12,9,21],reversed_cmp)的执行过程:
5 < 36
[5, 36, 12, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
12 < 36
[5, 12, 36, 9, 21]
12 > 5
[5, 12, 36, 9, 21]
9 < 12
[5, 9, 12, 36, 21]
9 > 5
[5, 9, 12, 36, 21]
21 > 12
[5, 9, 12, 21, 36]
21 < 36
[5, 9, 12, 36, 21]
#和排好序的列表中位数比较
2018-03-21
def count():
fs = []
def freeze(fs,val):
fs.append(val)
for i in range(1, 4):
freeze(fs,i*i)
return fs
fs = []
def freeze(fs,val):
fs.append(val)
for i in range(1, 4):
freeze(fs,i*i)
return fs
2018-03-20