8. 数据类型¶
8.1. datetime¶
8.1.1. timedelta¶
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
样例:
In [9]: from datetime import timedelta
In [10]: year = timedelta(days=365,hours=23,minutes=50,seconds=600)
In [11]: year.total_seconds()
Out[11]: 31622400.0
In [12]: year
Out[12]: datetime.timedelta(366)
In [13]: print(year)
366 days, 0:00:00
8.1.2. date¶
样例:
In [15]: from datetime import date
In [16]: date.min
Out[16]: datetime.date(1, 1, 1)
In [17]: date.max
Out[17]: datetime.date(9999, 12, 31)
In [21]: today = date.today()
In [22]: today
Out[22]: datetime.date(2018, 2, 11)
# 日期构造
In [24]: another_day = date(today.year,5,14)
In [25]: another_day
Out[25]: datetime.date(2018, 5, 14)
# 日期差值
In [26]: times = abs(today - another_day)
In [27]: type(times)
Out[27]: datetime.timedelta
In [28]: times.total_seconds
Out[28]: <function timedelta.total_seconds>
In [29]: times.total_seconds()
Out[29]: 7948800.0
# 日期转字符串的
In [30]: today.strftime("%Y-%m-%d")
Out[30]: '2018-02-11'
8.1.3. datetime¶
样例:
In [33]: d = date(2018,7,14)
In [34]: d
Out[34]: datetime.date(2018, 7, 14)
In [35]: t = time(12,30)
# 日期和时间构造一个datetime
In [36]: datetime.combine(d,t)
Out[36]: datetime.datetime(2018, 7, 14, 12, 30)
# 当前时间
In [37]: datetime.now()
Out[37]: datetime.datetime(2018, 2, 11, 15, 23, 19, 986889)
# utc时间,北京和utc时区差8个小时
In [38]: datetime.utcnow()
Out[38]: datetime.datetime(2018, 2, 11, 7, 23, 26, 978965)
In [39]: now = datetime.utcnow()
# 日期转字符串
In [41]: now.strftime("%Y-%m-%d %H:%M:%S")
Out[41]: '2018-02-11 07:23:39'
In [43]: now_str = '2018-02-11 07:23:39'
# 字符串转日期
In [44]: datetime.strptime(now_str,"%Y-%m-%d %H:%M:%S")
Out[44]: datetime.datetime(2018, 2, 11, 7, 23, 39)
8.1.4. time¶
样例:
In [46]: from datetime import time
In [47]: dt = time(hour=12, minute=34, second=56, microsecond=0)
# 指定下显示的精度程度
In [48]: dt.isoformat(timespec='microseconds')
Out[48]: '12:34:56.000000'
# 默认的,只是显示,时分秒
In [49]: dt.isoformat(timespec='auto')
Out[49]: '12:34:56'
8.1.5. 格式控制¶
8.2. calendar¶
提供日历功能
8.3. collectons¶
提供容器数据类型
8.3.1. ChainMap¶
提供用于快速链接多个映射,以便将它们视为单个单元。 它通常比创建新字典和运行多个update()调用要快得多。
样例:
In [53]: from collections import ChainMap
In [54]: c= ChainMap()
In [55]: d = c.new_child()
In [56]: e = c.new_child()
In [57]: e.maps[0]
Out[57]: {}
In [58]: e.maps[-1]
Out[58]: {}
In [59]: e.parents
Out[59]: ChainMap({})
In [60]: d['x'] = "abc"
In [61]: d
Out[61]: ChainMap({'x': 'abc'}, {})
In [62]: list(d)
Out[62]: ['x']
In [63]: d.items()
Out[63]: ItemsView(ChainMap({'x': 'abc'}, {}))
In [64]: dict(d)
Out[64]: {'x': 'abc'}
8.3.2. Counter¶
计数功能
# 导入 In [1]: from collections import Counter # 构造新对象 In [2]: cnt=Counter() # 开始计数 In [4]: for word in ["read","blue","green","blue","blue"]: ...: cnt[word]+=1 ...: # 查看 In [5]: cnt Out[5]: Counter({'blue': 3, 'green': 1, 'read': 1}) # 访问指定key In [6]: cnt["blue"] Out[6]: 3 # 直接设置 In [8]: cnt["yellow"] = 2 # 转化为dict In [9]: dict(cnt) Out[9]: {'blue': 3, 'green': 1, 'read': 1, 'yellow': 2} # 查看所有元素 In [14]: list(cnt.elements()) Out[14]: ['read', 'blue', 'blue', 'blue', 'green', 'yellow', 'yellow'] # 减法 In [15]: c = Counter(a=4, b=2, c=0, d=-2) In [16]: d = Counter(a=1, b=2, c=3, d=4) In [17]: c.subtract(d) In [18]: c Out[18]: Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6}) # 个数总和 In [20]: sum(c.values()) Out[20]: -6
8.3.3. deque¶
队列
常用方法:
# 导入
In [23]: from collections import deque
# 初始化
In [24]: d=deque("abc")
# 查看
In [25]: d
Out[25]: deque(['a', 'b', 'c'])
# 追加
In [26]: d.append("d")
# 查看
In [27]: d
Out[27]: deque(['a', 'b', 'c', 'd'])
# 左侧追加
In [28]: d.appendleft("0")
# 查看
In [29]: d
Out[29]: deque(['0', 'a', 'b', 'c', 'd'])
# 特定元素的个数
In [30]: d.count("0")
Out[30]: 1
# 批量追加
In [31]: d.extend("ef")
# 查看
In [32]: d
Out[32]: deque(['0', 'a', 'b', 'c', 'd', 'e', 'f'])
# 特定位置插入
In [33]: d.insert(4 ,"a")
# 查看
In [34]: d
Out[34]: deque(['0', 'a', 'b', 'c', 'a', 'd', 'e', 'f'])
# 右侧去除
In [35]: d.pop()
Out[35]: 'f'
# 查看
In [36]: d
Out[36]: deque(['0', 'a', 'b', 'c', 'a', 'd', 'e'])
# 左侧弹出
In [37]: d.popleft()
Out[37]: '0'
# 查看
In [38]: d
Out[38]: deque(['a', 'b', 'c', 'a', 'd', 'e'])
# 滚动2下,就是右边的元素放到第一个位置,在删除他原来的
In [39]: d.rotate(2)
# 查看
In [40]: d
Out[40]: deque(['d', 'e', 'a', 'b', 'c', 'a'])
获取到指定文件最后几行
In [44]: def tail (filename ,n=10):
...: with open(filename) as f:
...: return deque(f,n)
...:
8.3.4. defaultdict¶
默认字典,就是在原有字典的基础上提供默认值。
In [46]: from collections import defaultdict
In [47]: s = "zhaojiedi"
In [48]: d = defaultdict(int)
In [49]: for k in s:
...: d[k]+=1
...:
In [50]: d
Out[50]:
defaultdict(int,
{'a': 1, 'd': 1, 'e': 1, 'h': 1, 'i': 2, 'j': 1, 'o': 1, 'z': 1})
In [52]: d.items()
Out[52]: dict_items([('z', 1), ('h', 1), ('a', 1), ('o', 1), ('j', 1), ('i', 2), ('e', 1), ('d', 1)])
上面使用defaultdict指定int参数,如果没有值的话,会自动获取int的默认值0的。
8.3.5. namedtuple¶
给元组提供了名字的扩展
In [53]: from collections import namedtuple
In [54]: Point=namedtuple('Point',['x','y'])
In [55]: p=Point(11,2)
In [56]: p
Out[56]: Point(x=11, y=2)
In [57]: p.x +p.y
Out[57]: 13
In [58]: p[0] + p[1]
Out[58]: 13
# list 元素转化元组
In [59]: t=[1,2]
In [60]: Point._make(t)
Out[60]: Point(x=1, y=2)
# 命名元组转化有序字典
In [62]: p= Point(x=11,y=2)
In [63]: p._asdict()
Out[63]: OrderedDict([('x', 11), ('y', 2)])
# 获取字段
In [64]: p._fields
Out[64]: ('x', 'y')
# 获取属性值
In [66]: getattr(p,'x')
Out[66]: 11
8.3.6. OrderedDict¶
有序字典与普通词典一样,但它们记住插入项的顺序。在遍历一个有序字典时,这些项将按其第一次添加的顺序返回。
# 导入
In [67]: from collections import OrderedDict
In [68]: d= {'banana':3 , 'appale': 4 , 'orange':2}
# 根据元素的key来排序
In [69]: e = OrderedDict(sorted(d.items(),key=lambda t:t[0]))
In [70]: e
Out[70]: OrderedDict([('appale', 4), ('banana', 3), ('orange', 2)])
# 根据元素的value来排序
In [71]: f = OrderedDict(sorted(d.items(),key=lambda t:t[1]))
In [72]: f
Out[72]: OrderedDict([('orange', 2), ('banana', 3), ('appale', 4)])
8.4. collections.abc¶
这个模块提供抽象基类,可以用来测试是否一个类提供了一个特定的接口;
8.5. heapq¶
这个模块提供了一个堆队列算法的实现,也称为优先级队列算法。
# 导入
In [87]: from heapq import heappush,heappop
In [88]: h=[]
# 添加一个元组
In [89]: heappush(h,(5,'write code'))
In [90]: h
Out[90]: [(5, 'write code')]
# 在加一个
In [91]: heappush(h,(7,'write book'))
In [92]: h
Out[92]: [(5, 'write code'), (7, 'write book')]
# 弹出一个
In [93]: heappop(h)
Out[93]: (5, 'write code')
# 查看
In [94]: h
Out[94]: [(7, 'write book')]
8.6. bisect¶
此模块提供了支持以排序的顺序维护列表,而不必在每次插入之后对列表进行排序。
样例:
In [110]: from bisect import bisect , bisect_left
In [111]: def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
...: i = bisect(breakpoints, score)
...: return grades[i]
...:
In [112]:
In [112]: [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
Out[112]: ['F', 'A', 'C', 'C', 'B', 'A', 'A']
上面的使用4 个break point 将区间划分为5个, 每个对应一个等级,使用bisect去查找对应索引。
8.7. array¶
这个模块定义了一个可以紧凑地表示基本值数组的对象类型:字符、整数、浮点数。
8.8. weakref¶
这个模块允许Python程序员创建对象的弱引用
8.9. types¶
此模块定义实用工具函数,以帮助动态创建新类型。
8.10. copy¶
此模块提供深copy和浅copy功能
样例:
In [113]: class Point:
...: pass
...:
In [114]: p= Point()
In [116]: import copy
In [117]: p2 =copy.copy(p)
In [119]: p3 = copy.deepcopy(p)
8.11. pprint¶
数据显示美化打印
方法: pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False)
>>> import json
>>> import pprint
>>> from urllib.request import urlopen
>>> with urlopen('http://pypi.python.org/pypi/Twisted/json') as url:
... http_info = url.info()
... raw_data = url.read().decode(http_info.get_content_charset())
>>> project_info = json.loads(raw_data)
>>> pprint.pprint(project_info)
{'info': {'_pypi_hidden': False,
'_pypi_ordering': 125,
'author': 'Glyph Lefkowitz',
'author_email': 'glyph@twistedmatrix.com',
'bugtrack_url': '',
'cheesecake_code_kwalitee_id': None,
'cheesecake_documentation_id': None,
'cheesecake_installability_id': None,
'classifiers': ['Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 2 :: Only'],
'description': 'An extensible framework for Python programming, with '
'special focus\r\n'
'on event-based network programming and multiprotocol '
'integration.',
'docs_url': '',
'download_url': 'UNKNOWN',
'home_page': 'http://twistedmatrix.com/',
'keywords': '',
'license': 'MIT',
'maintainer': '',
'maintainer_email': '',
'name': 'Twisted',
'package_url': 'http://pypi.python.org/pypi/Twisted',
'platform': 'UNKNOWN',
'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0',
'requires_python': None,
'stable_version': None,
'summary': 'An asynchronous networking framework written in Python',
'version': '12.3.0'},
'urls': [{'comment_text': '',
'downloads': 71844,
'filename': 'Twisted-12.3.0.tar.bz2',
'has_sig': False,
'md5_digest': '6e289825f3bf5591cfd670874cc0862d',
'packagetype': 'sdist',
'python_version': 'source',
'size': 2615733,
'upload_time': '2012-12-26T12:47:03',
'url': 'https://pypi.python.org/packages/source/T/Twisted/Twisted-12.3.0.tar.bz2'},
{'comment_text': '',
'downloads': 5224,
'filename': 'Twisted-12.3.0.win32-py2.7.msi',
'has_sig': False,
'md5_digest': '6b778f5201b622a5519a2aca1a2fe512',
'packagetype': 'bdist_msi',
'python_version': '2.7',
'size': 2916352,
'upload_time': '2012-12-26T12:48:15',
'url': 'https://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.3.0.win32-py2.7.msi'}]}
>>> pprint.pprint(project_info, depth=2)
{'info': {'_pypi_hidden': False,
'_pypi_ordering': 125,
'author': 'Glyph Lefkowitz',
'author_email': 'glyph@twistedmatrix.com',
'bugtrack_url': '',
'cheesecake_code_kwalitee_id': None,
'cheesecake_documentation_id': None,
'cheesecake_installability_id': None,
'classifiers': [...],
'description': 'An extensible framework for Python programming, with '
'special focus\r\n'
'on event-based network programming and multiprotocol '
'integration.',
'docs_url': '',
'download_url': 'UNKNOWN',
'home_page': 'http://twistedmatrix.com/',
'keywords': '',
'license': 'MIT',
'maintainer': '',
'maintainer_email': '',
'name': 'Twisted',
'package_url': 'http://pypi.python.org/pypi/Twisted',
'platform': 'UNKNOWN',
'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0',
'requires_python': None,
'stable_version': None,
'summary': 'An asynchronous networking framework written in Python',
'version': '12.3.0'},
'urls': [{...}, {...}]}
8.12. reprlib¶
reprlib模块提供了对生成的字符串的大小限制生产对象表示的一种手段
8.13. enum¶
枚举
In [135]: from enum import Enum,auto
In [137]: class Color(Enum):
...: RED=1
...: GREEN =2
...: BLUE =auto()
...:
In [138]: print(Color.RED)
Color.RED
In [139]: print(Color.RED.name)
RED
In [140]: print(Color.RED.value)
1
# 判断
In [141]: Color.RED == Color(1)
Out[141]: True
# 给枚举加唯一条件
In [142]: from enum import Enum , unique
In [143]: @unique
...: class MIsstake(Enum):
...: one=1
...: two=2
...: three=3
...: four=3
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-143-8f8798c8b548> in <module>()
1 @unique
----> 2 class MIsstake(Enum):
3 one=1
4 two=2
5 three=3
D:\Users\Administrator\Anaconda3\lib\enum.py in unique(enumeration)
832 ["%s -> %s" % (alias, name) for (alias, name) in duplicates])
833 raise ValueError('duplicate values found in %r: %s' %
--> 834 (enumeration, alias_details))
835 return enumeration
836
ValueError: duplicate values found in <enum 'MIsstake'>: four -> three
# 遍历
In [145]: [ name for name, member in Color.__members__.items() ]
Out[145]: ['RED', 'GREEN', 'BLUE']
8.13.1. IntEnum¶
整型枚举
In [146]: from enum import IntEnum
...: class Shape(IntEnum):
...: circle =1
...: square =2
...:
In [147]: Shape.circle ==1
Out[147]: True
8.13.2. IntFlag¶
整型标记
In [150]: class Perm(IntFlag):
...: R =4
...: W =2
...: X =1
...:
...:
In [151]: Perm.R
Out[151]: <Perm.R: 4>
In [152]: Perm.R ==4
Out[152]: True
In [153]: Perm.R | Perm.W
Out[153]: <Perm.R|W: 6>
In [154]: Perm.R | Perm.W ==6
Out[154]: True
8.14. Flag¶
标记
In [160]: from enum import Flag
...: class Color(Flag):
...: red=auto()
...: blue=auto()
...: green=auto()
...:
In [161]: Color.red
Out[161]: <Color.red: 1>
In [162]: Color.red ==1
Out[162]: False
In [163]: Color.blue
Out[163]: <Color.blue: 2>
In [164]: Color.green
Out[164]: <Color.green: 4>
使用Flag,每个item都是按照1,2,4,8,16这样的值。
这种flag的主要用于后续有异或运算的情况下。