列表筛选符合条件的元素
data= [1,2,3,4,5,6,7,8,9]
方法一:内置函数
filter(lambda x:x>=0,data)
方法二:列表解析式
[x for x in data if x>=0]
PS:timeit()
计算程序运行的时间
字典筛查符合条件的元素
key,value
字典解析:
In [97]: {k:v for k,v in d.iteritems() if v>90}
Out[97]: {6: 91, 11: 100}
集合解析:
set()的使用
In [92]: data
Out[92]: [4, -8, -2, -1, -9, 3, -9, -4, 0, 7]
In [93]: s=set(data)
In [94]: {x for x in s if x%3==0}
Out[94]: {-9, 0, 3}
元组的使用及可提高文件可读性:
定义枚举类型,定义数值常量
In [98]: students=('jam',38,'byx54192@gmail.com')
In [101]: students
Out[101]: ('jam', 38, 'byx54192@gmail.com')
name,age,email=range(4)
In [104]: students[name]
Out[104]: 'jam'
使用python 标准库中collections.namedtuple替代内置的tuple
In [124]: student=namedtuple('student',['name','age','email'])
In [125]: s=student('jame',122,'byx54192@qq.com')
In [126]: s.name
Out[126]: 'jame'
In [127]: s.age
Out[127]: 122
In [128]: s.email
Out[128]: 'byx54192@qq.com'
如何统计序列中元素的出现频度:
方法一:迭代
In [141]: data=[randint(0,20)for _ in range(30)]
In [142]: c=dict.fromkeys(data,0)
In [143]: for x in data:
...: c[x]+=1
方法二:内置函数:
In [156]: from collections import Counter
In [157]: c2=Counter(data)
In [158]: c2.most_common()
实践:
import re
In [188]: text=open('jiandan.py').read()
In [189]: c3=Counter(re.split('\W+',text))#正则表达式来分割字符串
In [190]: c3.most_common(3)
Out[190]: [('h', 15), ('0', 11), ('f', 11)]
方法一;
In [188]: text=open('jiandan.py').read()
In [189]: c3=Counter(re.split('\W+',text))
In [190]: c3.most_common(3)
Out[190]: [('h', 15), ('0', 11), ('f', 11)]
用元组实现成绩排名:
In [205]: zip(d.itervalues(),d.iterkeys())
Out[205]: [(61, 'd'), (82, 'f'), (93, 'h'), (85, 'k'), (60, 's'), (98, 'x')]
iter迭代对象的使用减少对内存的消耗。
In [205]: zip(d.itervalues(),d.iterkeys())
Out[205]: [(61, 'd'), (82, 'f'), (93, 'h'), (85, 'k'), (60, 's'), (98, 'x')]
元组的值的排序
In [207]: sorted(d.items(),key=lambda x:x[1])
In [209]: t=zip(d.itervalues(),d.iterkeys())
In [211]: sorted(t)
实现可迭代对象和迭代器对象
可迭代的对象案例代码:
import requests
from collections import Iterable,Iterator
class WeatherIterator(Iterator):
def __init__(self,cities):
self.cities = cities
self.index=0
def __next__(self):
if self.index==len(self.cities):
raise StopIteration
city=self.cities[self.index]
self.index+=1
return self.getWeather(city)
def getWeather(self,city):
r=requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city='+city)
data=r.json()['data']['forecast'][0]
return (city,data['low'],data['high'])
class WeatherIterable(Iterable):
def __init__(self,cities):
self.cities=cities
#print(self.cities)
def __iter__(self):
return (WeatherIterator(self.cities))
data=['北京','银川','上海','合肥']
for info in WeatherIterable(data):
print(info)
实现可迭代对象的类,它能迭代出给定范围内所有素数:
class PrimeNumber:
"""get PrimeNumber from range()"""
def __init__(self,start,end):
self.start =start
self.end=end
def isPrimeNum(self,k):
for i in range(2,k):
if k<2:
return False
if k%i==0:
return False
return True
def __iter__(self):
for k in range(self.start,self.end+1):
if self.isPrimeNum(k):
yield (k)
for x in PrimeNumber(0,100):
print (x)
反向迭代及反向迭代的实现:
构造器函数:
class FloatRange:
def __init__(self,start,end,step=0.1):
self.start=start
self.end=end
self.step=step
def __iter__(self):
t= self.start
while t<=self.end:
yield t
t+=self.step
def __reversed__(self):
t=self.end
while t>=self.start:
yield t
t-=self.step
for x in reversed(FloatRange(1.0,10.0,4.0)):
print (x)
迭代器实现切片操作
In [63]: from itertools import islice
In [63]: for line in islice(f,10,50,step=1):
...: print(line)
内置函数zip(),合并多个集合。
利用模块from itertools import chain
实现不同集合合并
多分隔符的字符串处理
re.split(r'[*]',strings)
string.split()
正则表达式替换字符串中的字段
In [7]: ha=re.sub('(\d{4})-(\d{2})-(\d{2})',r'\2/\3/\1',log)
In [8]: ha
Out[8]: '19/15/2016 fhjkadfhajshflafds\n35/12/2017
fhjkadfhajshflafds\n12/14/2014 fhjkadfhajshflafds\n45/17/2012
fhjkadfhajshflafds\n12/18/2014 fhjkadfhajshflafds\n02/18/2016
fhjkadfhajshflafds\n01/19/2017 fhjkadfhajshflafds\n13/12/2019
fhjkadfhajshflafds\n15/11/2010 fhjkadfhajshflafds\n16/14/2011
fhjkadfhajshflafds\n\n'
In [9]: ha=re.sub('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',r'\g<year>/\
...: g<month>/\g<day>',log)
In [10]: ha
Out[10]: '2016/19/15 fhjkadfhajshflafds\n2017/35/12
fhjkadfhajshflafds\n2014/12/14 fhjkadfhajshflafds\n2012/45/17
fhjkadfhajshflafds\n2014/12/18 fhjkadfhajshflafds\n2016/02/18
fhjkadfhajshflafds\n2017/01/19 fhjkadfhajshflafds\n2019/13/12
fhjkadfhajshflafds\n2010/15/11 fhjkadfhajshflafds\n2011/16/14
fhjkadfhajshflafds\n\n'
字符串的拼接 join()方法:
In [28]: ''.join([str(x) for x in d])
Out[28]: 'adcdsfsdfsdfsf12212332424'
生成器表达式:([str(x) for x in d]) #节省内存,推荐使用
字符串居中,居左,居右对齐。
s='abc'
s.ljust(20)
s.rjust(20)
format(s,'<20')#左对齐
format(s,'>20')#右对齐
format(s,'^20')#居中对齐
去掉字符串中的某些字符
strip()
s.lstrip()
s.rstrip()
s.replace('a','b')#b 换 a
re.sub('[\r\t]','',string)
去掉字符串中的音调符号
import string
In [33]: s.translate(None,'\r\t\n')
读写文本及编码解码问题
encode()
decode()
json文件读写
l='adfsfafdsadf'
with open('demo.json','rb') as f:
json.dump(l,f)
json.load()
json.dumps(l,separators=[',',':']
去除json格式中的字符
如何派生内置不可变类型并修改实例化行为:
定义一种新的元组,对于传入的可迭代对象,只保留其中int类型且值大于0的元素