1本砰、如何調(diào)整字符串中文本的格式碴裙?
方法:使用 re.sub
In [113]: import re
In [111]: day = '2017-07-24'
In [115]: re.sub('(\d{4})-(\d{2})-(\d{2})',r'\2/\3/\1',day)
Out[115]: '07/24/2017'
2、如何將多個小字符串拼接成一個大字符串点额?
方法一:使用 “+” 操作符
In [118]: s = ['sdfdsfs','sdfewv','cvxbxcz','yetruywow']
In [119]: string = ''
In [120]: for st in s:
...: string+=st
...: print string
sdfdsfs
sdfdsfssdfewv
sdfdsfssdfewvcvxbxcz
sdfdsfssdfewvcvxbxczyetruywow # 此方法會有較大的內(nèi)存消耗
方法二:使用 str.join
In [118]: s = ['sdfdsfs','sdfewv','cvxbxcz','yetruywow']
In [121]: ''.join(s)
Out[121]: 'sdfdsfssdfewvcvxbxczyetruywow'
In [122]: s1 = ['dsd','adev',6545,7711,'ewrewc']
In [123]: ''.join((str(x) for x in s1)) # 對有數(shù)字的列表進(jìn)行處理
Out[123]: 'dsdadev65457711ewrewc'
3舔株、如何對字符串進(jìn)行左、右还棱、居中對齊载慈?
方法一:使用 str.ljust,str.rjust,str.center
In [124]: s = 'going'
In [125]: s.ljust(20,'-')
Out[125]: 'going---------------'
In [126]: s.rjust(20,'-')
Out[126]: '---------------going'
In [127]: s.center(20,'-')
Out[127]: '-------going--------'
In [133]: d = {'housename':'marryCenter','width':30,'height':20.55,'long':30}
In [137]: for k in d:
...: print k.ljust(max(map(len,d.keys()))),':',d[k]
width : 30
housename : marryCenter
long : 30
height : 20.55
方法二:使用 format
In [130]: format(s,'<20')
Out[130]: 'going '
In [131]: format(s,'>20')
Out[131]: ' going'
In [132]: format(s,'^20')
Out[132]: ' going '
4、如何去掉字符串中不需要的字符珍手?
方法一:使用 strip
In [145]: s = '///abc*****'
In [148]: s.strip('/*')
Out[148]: 'abc'
方法二:切片拼接
In [149]: s = 'carry?me'
In [151]: s[:5]+s[6:]
Out[151]: 'carryme'
方法三:使用 str.replace
In [152]: s = 'sweep\nhouse\n'
In [154]: s.replace('\n','')
Out[154]: 'sweephouse'
方法四:使用 re.sub
In [158]: s = 'oh\tmy\ngod'
In [159]: re.sub('[\t\n]','',s)
Out[159]: 'ohmygod'
方法五:使用 str.translate
In [165]: s = 'not\na\tfellower\r'
In [167]: s.translate(None,'\t\r\n')
Out[167]: 'notafellower'
5办铡、如何派生內(nèi)置不可變類型并修改實(shí)例化行為?
方法:實(shí)現(xiàn) __new__ 方法
例子:將列表中的數(shù)字篩選出來琳要。
In [6]: class IntTuple(tuple):
...: def __new__(cls,iterable):
...: l = (x for x in iterable if isinstance(x,int))
...: return super(IntTuple,cls).__new__(cls,l)
...: def __init__(self,iterable):
...: super(IntTuple,self).__init__(iterable)
In [7]: t = IntTuple([1,0,-1,7,'HAH'])
In [8]: print t
(1, 0, -1, 7)
tips:實(shí)例由內(nèi)置方法__new__創(chuàng)建寡具,所以修改__new__方法就可實(shí)現(xiàn)新型元組。
6稚补、如何為創(chuàng)建大量實(shí)例節(jié)省內(nèi)存童叠?
方法:定義類的__slot__屬性
例子:定義一個學(xué)生類的兩種方法
In [12]: class Student1(object):
...: def __init__(self,mid,name,age,sex):
...: self.mid = mid
...: self.name = name
...: self.age = age
...: self.sex = sex
In [13]: class Student2(object): # 多了一個__slots__屬性
...: __slots__ = ['mid','name','age','sex']
...: def __init__(self,mid,name,age,sex):
...: self.mid = mid
...: self.name = name
...: self.age = age
...: self.sex = sex
In [14]: s1 = Student1('01','kim','20','male')
In [16]: s2 = Student2('01','kim','20','male')
In [21]: set(dir(s1)) - set(dir(s2))
Out[21]: {'__dict__', '__weakref__'}# Student1比Student2多出兩個屬性,所以內(nèi)存占用方面會比較大
In [23]: s1.email = '123@mail.com' # Student1支持動態(tài)綁定
In [24]: s1.__dict__
Out[24]:
{'age': '20',
'email': '123@mail.com',
'mid': '01',
'name': 'kim',
'sex': 'male'}
In [28]: s2.email = '123@mail.com' # Student2不支持動態(tài)綁定
AttributeError: 'Student2' object has no attribute 'email'
tips:__slots__屬性可以節(jié)省__dict__屬性课幕,并且不支持動態(tài)綁定厦坛,從而達(dá)到減少內(nèi)存開銷的目的。
7乍惊、如何讓類支持比較操作杜秸?
方法一:類內(nèi)部實(shí)現(xiàn)__lt__,__le__,__gt__,__ge__,__eq__, __ne__
In [36]: class Rectangle(object):
...: def __init__(self,width,height):
...: self.width = width
...: self.height = height
...: def area(self):
...: return self.width*self.height
...: def __gt__(self,obj): # 大于
...: print '__gt__'
...: return self.area() > obj.area()
...: def __eq__(self,obj): # 等于
...: print '__eq__'
...: return self.area() == obj.area()
In [37]: r1 = Rectangle(5,5)
In [38]: r2 = Rectangle(4,6)
In [39]: r1 > r2
__gt__
Out[39]: True
In [40]: r1 == r2
__eq__
Out[40]: False
方法二:使用 total_ordering 簡化
In [43]: from functools import total_ordering
In [44]: @total_ordering
...: class Rectangle(object):
...: def __init__(self,width,height):
...: self.width = width
...: self.height = height
...: def area(self):
...: return self.width*self.height
...: def __lt__(self,obj): # 小于
...: print '__lt__'
...: return self.area() < obj.area()
...: def __eq__(self,obj): # 等于
...: print '__eq__'
...: return self.area() == obj.area()
In [45]: r1 = Rectangle(5,6)
In [46]: r2 = Rectangle(3,9)
In [47]: r1 > r2 # 可以使用大于
__lt__
__eq__
Out[47]: True
tips:使用 @total_ordering 修飾,可以根據(jù)給出的兩個運(yùn)算符進(jìn)行推導(dǎo)润绎。
方法二升級:定義一個基類支持多類型比較
In [48]: from abc import ABCMeta,abstractmethod
...: @total_ordering
...: class Shape(object):
...: @abstractmethod #子類必須實(shí)現(xiàn)的方法
...: def area(self):
...: pass
...: def __lt__(self,obj):
...: print '__lt__'
...: if not isinstance(obj,Shape):#判斷類型是否有誤
...: raise TypeError('obj is not Shape!')
...: return self.area() < obj.area()
...: def __eq__(self,obj):
...: print '__eq__'
...: if not isinstance(obj,Shape):
...: raise TypeError('obj is not Shape!')
...: return self.area() == obj.area()
In [49]: class Rectangle(Shape):
...: def __init__(self,width,height):
...: self.width = width
...: self.height = height
...: def area(self):
...: return self.width*self.height
In [50]: class Circle(Shape):
...: def __init__(self,r):
...: self.r = r
...: def area(self):
...: return self.r**2*3.14
In [51]: r1 = Rectangle(9,5)
In [52]: c1 = Circle(4)
In [53]: print r1 > c1
__lt__
False
tips:通過定義基類撬碟,可以比較多種類的面積大小诞挨,就達(dá)到簡化程序的目的。
8小作、如何在一個 for 語句中迭代多個可迭代對象亭姥?
方法一:zip 函數(shù)進(jìn)行并行操作
例子:同時迭代4個列表
In [71]: data1 = [randint(60,100) for _ in xrange(10)]
In [72]: data2 = [randint(60,100) for _ in xrange(10)]
In [73]: data3 = [randint(60,100) for _ in xrange(10)]
In [75]: data4 = [randint(60,100) for _ in xrange(10)]
In [74]: for x,y,z,r in zip(data1,data2,data3,data4):# 解包
...: total = x+y+z+r
...: print total
198
238
234
235
...
方法二:使用 chain 進(jìn)行串行操作
例子:數(shù)據(jù)同上,統(tǒng)計大于80的項(xiàng)
In [78]: from itertools import chain
In [79]: count = 0
In [81]: for s in chain(data1,data2,data3,data4):
...: if s>80:
...: count += 1
...:
In [82]: count
Out[82]: 17