博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Note_python(01)
阅读量:2491 次
发布时间:2019-05-11

本文共 3378 字,大约阅读时间需要 11 分钟。

映射、字典

映射类型内建函数

  • dict
    Error:
    Python核心编程(第二版)p170
>>> dict([['x', 1], ['y', 2]]){
'y': 2, 'x': 1}实际输出测试:>>> dict([['x', 1], ['y', 2]]){
'y': 2, 'x'Traceback (most recent call last): File "
", line 1, in
IOError: [Errno 0] ErrorCorrect:>>> dict((['x', 1], ['y', 2])){
'y': 2, 'x': 1}

三元操作符

X if C else Y

eg.

>>> x, y = 4, 5>>> smaller = x if x < y else y>>> smaller4

enumerate()内建函数

eg.

>>> numlist = ['a', 'b', 'c']>>> for index, i in enumerate(numlist):...     print '%d %s' % (index+1, i)...1 a2 b3 c

zip()

eg.

>>> numlist['a', 'b', 'c']>>> strlist = [1, 2, 3]>>> strlist[1, 2, 3]>>> for num, str in zip(numlist, strlist):...     print '%d \t %s' % (str, num)...1        a2        b3        c

再谈else语句

在Python中,else语句也可以在while和for循环中使用。在循环中使用时,else子句只在循环完成后执行,但是break语句会跳过else块。

eg.

>>> for i in range(10, 21):...     if i%2 == 0:...             print '[%d] \t [%d]' % (i, i%2)...             break... else:...     print 'Break Test successful!'...[10]     [0]

迭代器

  • 0、创建迭代器
    iter(obj)
    iter(func, sentinel)

迭代器是一个有next()方法的对象,通过next()可以取出所需要的下一个项,当所有的项被取出后,就会报一个StopIteration异常,这并不是一个错误,只是告诉外部调用者,迭代完成。

* 1、序列
一个for循环的完整工作是这样的:

>>> seq = ('q121', 132, 'dad')>>> seq = iter(seq)>>> while True:...     try:...         i = seq.next()...     except StopIteration:...         break...     print '[%s]' % i...[q121][132][dad]
  • 2、字典
    eg.
    dict.iterkeys() dict.itervalues() dict.iteritems()
>>> seq = {
'a': 1, 'b': 2}>>> for i in seq.iterkeys():... print i...ab>>> for i in seq.values():... print i...12>>> for i in seq.items():... print i...('a', 1)('b', 2)>>> for i, j in seq.items():... print '[%s] \t [%s]' % (i, j)...[a] [1][b] [2]

列表表达式

eg.

>>> lambda x: x ** 2, range(6)(
at 0x00000000052AEBA8>, [0, 1, 2, 3, 4, 5])>>> map(lambda x: x ** 2, range(6))[0, 1, 4, 9, 16, 25]>>> [x ** 2 for x in range(6)][0, 1, 4, 9, 16, 25]>>> [x ** 2 for x in range(6) if x % 2 == 0][0, 4, 16]

列表解析式的判断部分默认为真:

eg.

>>> [x ** 2 for x in range(6) if x % 2 ] // 真[1, 9, 25]>>> [x ** 2 for x in range(6) if x % 2 == 0] // 假[0, 4, 16]

矩阵样例

同一个例子,为啥输出不一样!

eg.

>>> [(x+1, y+1) for x in range(3) for y in range(5)][(1, 1)Traceback (most recent call last):  File "
", line 1, in
IOError: [Errno 0] Error>>> [(x+1, y+1) for x in range(3) for y in range(5)][(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]## 追加>>> str['suahduaihsdu']>>> [i for word in str for i in word]['s', 'u', 'a', 'h', 'd', 'u', 'a', 'i', 'h', 's', 'd', 'u']

os.stat()

eg.

>>> import os>>> print os.stat("/root/python/zip.py")(33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895)>>> print os.stat("/root/python/zip.py").st_mode   #权限模式33188>>> print os.stat("/root/python/zip.py").st_ino   #inode number2033080>>> print os.stat("/root/python/zip.py").st_dev    #device26626>>> print os.stat("/root/python/zip.py").st_nlink  #number of hard links1>>> print os.stat("/root/python/zip.py").st_uid    #所有用户的user id0>>> print os.stat("/root/python/zip.py").st_gid    #所有用户的group id0>>> print os.stat("/root/python/zip.py").st_size  #文件的大小,以位为单位864>>> print os.stat("/root/python/zip.py").st_atime  #文件最后访问时间1297653596>>> print os.stat("/root/python/zip.py").st_mtime  #文件最后修改时间1275528102>>> print os.stat("/root/python/zip.py").st_ctime  #文件创建时间1292892895

转载地址:http://qgarb.baihongyu.com/

你可能感兴趣的文章
asp.net core结合Gitlab-CI实现自动化部署
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.7 版本发布
查看>>
EasyNVR H5无插件摄像机直播解决方案前端解析之:关于直播页面和视频列表页面切换的问题...
查看>>
django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
查看>>
redis事务
查看>>
Java_基础语法之dowhile语句
查看>>
HDU 2175 汉诺塔IX
查看>>
PAT 甲级 1021 Deepest Root
查看>>
查找代码错误.java
查看>>
vc获取特殊路径(SpecialFolder)
查看>>
单例模式
查看>>
int(3)和int(11)区别
查看>>
201521123061 《Java程序设计》第十一周学习总结
查看>>
代码小思考
查看>>
Unity中的销毁方法
查看>>
ceph删除pool提示(you must first set the mon_allow_pool_delete config option to true)解决办法...
查看>>
2016-7-15(1)使用gulp构建一个项目
查看>>
CSS 设计指南(第3版) 初读笔记
查看>>
markdown学习/mou
查看>>
CentOS 搭建 LAMP服务器
查看>>