py3需要先導入模塊
from functools import rduce
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
from functools import rduce
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2015-11-10
注意了 在py3中需要使用 list()轉換map()
print(list(map(format_name, ['adam', 'LISA', 'barT']))
print(list(map(format_name, ['adam', 'LISA', 'barT']))
2015-11-10
一定要用 super(Student, self).__init__(name, gender) 去初始化父类,否则,继承自 Person 的 Student 将没有 name 和 gender。
2015-11-10
x在前則-1
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-11-09
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
return 'A-优秀' if self.__score>=90 else 'C-不及格' if self.__score<60 else 'B-及格'
# -*- coding: utf-8 -*-
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
return 'A-优秀' if self.__score>=90 else 'C-不及格' if self.__score<60 else 'B-及格'
2015-11-09
def format_name(s):
return s[0].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'])
return s[0].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'])
2015-11-09
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'])
2015-11-08
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
elif self.score == s.score:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
2015-11-07