参考自
#map(fun,list)
print (list(map(str,list(range(10)))))
print (map(str,range(10)))
#将字符串数字转化为int
from functools import reduce
def str2int(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
return reduce(fn, list(map(char2num, s)))
print (str2int('123'))
#map(fun,l1) 对list中所有数执行函数fun
def change(s):
return s[0].upper()+s[1:].lower()
l=['lias','DDVd','FAs']
l2=map(change,l)
print (list(l2))
#reduce(fun,l2) 对list中所有数累计执行fun,fun(x,y)
def prod(l):
def pro(x,y):
return x*y
return reduce(pro,l)
print (prod([1,2,3])) #累乘 6
#filter(fun,list) fun函数返回true则保留,否则舍弃
def is_odd(n):
return n%2==0
print (list(filter(is_odd,[1,2,3,4,5,6]))) #2,4,6
print (sorted(['A','c','B','d'],key=str.lower))
print (sorted(['A','c','B','d'],key=str.lower,reverse=True))
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def cmp(x):
return x[0]
print (sorted(L,key=cmp))
#匿名函数 lambda x :x*x == def x: return x*x
print (list(map(lambda x:x*x,[1,2,3])))
#类,,在函数或变量前加__就是定义为内部变量
class Stu(object):
def __init__(self,name,score):
self.name=name
#self.__name=name 内部变量外部不能通过stu.__name访问
self.score=score
def print_sco(self):
print ('%s: %d'% (self.name,self.score))
def get_grade(self):
if (self.score>=90):
return 'a'
else:
return 'not a'
stu = Stu('baiy',90)
stu.print_sco()
stu.__name='xxx' #可以通过此方式为类添加成员变量,但是不是内部的
print (stu.__name)
class Animal(object):
def run(self):
print ('ani is run')
class Dog(Animal):
pass
class Cat(Animal):
def run(self):
print ('cat is run')
dog=Dog()
dog.run() #ani is run
cat=Cat()
cat.run() #cat is run 覆盖
type(123)==type(456) #True
type(123)==int #True
type('abc')==type('123') #True
type('abc')==str #True
type('abc')==type(123) #False
dir('ABC') #获取对象的所有属性和方法
len('ABC') == 'ABC'.__len__() #所有想调用len(),只需要在类中实现__len__()函数
#类属性和实例属性
class Student(object):
name = 'Student'
s = Student() # 创建实例s
print(s.name) # 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性
#Student
print(Student.name) # 打印类的name属性
Student
s.name = 'Michael' # 给实例绑定name属性
print(s.name) # 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性
#Michael
print(Student.name) # 但是类属性并未消失,用Student.name仍然可以访问
#Student
del s.name # 如果删除实例的name属性
print(s.name) # 再次调用s.name,由于实例的name属性没有找到,类的name属性就显示出来了
#Student
#给实例绑定一个方法 只对此实例有效
s = Student()
def set_age(self,age):
self.age=age
from types import MethodType
s.set_age=MethodType(set_age,s)
s.set_age(25)
print (s.age)
#给类绑定一个方法,对所有实例有效
def set_sex(self,sex):
self.sex=sex
Student.set_sex=set_sex
s.set_sex('man')
print(s.sex)
#__slots__现在成员变量
class People(object):
__slots__=('name','age')
peop=People()
#peop.x=100 错误
#property用法
class Student(object):
@property
def birth(self):
return self._birth
@birth.setter
def birth(self, value):
self._birth = value
@property
def age(self):
return 2015 - self._birth
#多重继承
class Runnable(object):
pass
class Dog(Animal,Runnable):
pass
f=open('text.txt','r')
ss=f.readline()
s=f.readlines()
print(ss)
print (s)
print (s[3]=='\n')
f.close()
f = open('test.txt', 'w')
f.write('Hello, world!')
f.close()
#stringio
from io import StringIO
f = StringIO()
f.write('hello')
f.write(' ')
f.write('world!')
print(f.getvalue())
#hello world!