Python学习小记(2)---[list, iterator, and, or, zip, dict.keys]
1.List行为
可以用 alist[:] 相当于 alist.copy() ,可以创建一个 alist 的 shallo copy,但是直接对 alist[:] 操作却会直接操作 alist 对象
- >>> alist = [1,2,3]
- >>> blist = alist[:] #assign alist[:] to blist
- >>> alist
- [1, 2, 3]
- >>> blist
- [1, 2, 3]
- >>> blist[2:] = ['a', 'b', 'c'] #allter blist
- >>> alist
- [1, 2, 3]
- >>> blist
- [1, 2, 'a', 'b', 'c']
- >>> alist[:] = ['a', 'b', 'c'] #alter alist[:]
- >>> alist
- ['a', 'b', 'c']
2.循环技巧
- #list
- >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
- >>> for k, v in knights.items():
- ... print(k, v)
- ...
- gallahad the pure
- robin the brave
- #zip函数
- >>> questions = ['name', 'quest', 'favorite color']
- >>> answers = ['lancelot', 'the holy grail', 'blue']
- >>> for q, a in zip(questions, answers):
- ... print('What is your {0}? It is {1}.'.format(q, a))
- ...
- What is your name? It is lancelot.
- What is your quest? It is the holy grail.
- What is your favorite color? It is blue.
- #reversed & sorted
- #Note: 这两个函数不修改参数本身,返回一个iterator
- #reversed
- >>> for i in reversed(range(1, 10, 2)):
- ... print(i)
- ...
- 9
- 7
- 5
- 3
- 1
- #sorted
- >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
- >>> for f in sorted(set(basket)):
- ... print(f)
- ...
- apple
- banana
- orangez
- pear
3.
enumerate()函数可以把创建ist,str的可迭代对象,迭代对象每次返回一个(index, value),形式的元组
- >>> astr = 'abc'
- >>> alist = [1,2,3]
- >>> enumerate(astr)
- <enumerate object at 0x0374D760>
- >>> enumerate(alist)
- <enumerate object at 0x0374D698>
- >>> def print_iterator(iterator):
- ... for ele in iterator:
- ... print(ele)
- ...
- >>> print_iterator(astr)
- a
- b
- c
- >>> print_iterator(enumerate(astr))
- (0, 'a')
- (1, 'b')
- (2, 'c')
- >>> print_iterator(enumerate(alist))
- (0, 1)
- (1, 2)
- (2, 3)
- >>>
4.zip()示例
- >>> a = [1,2,3]
- >>> b = ['a', 'b', 'c']
- >>> c = ['one', 'two', 'three']
- >>> a,b,c
- ([1, 2, 3], ['a', 'b', 'c'], ['one', 'two', 'three'])
- >>>
- >>> def print_iterator(iterator):
- ... for ele in iterator:
- ... print(ele)
- ...
- >>>
- >>> print_iterator(zip(a))
- (1,)
- (2,)
- (3,)
- >>> print_iterator(zip(a,b))
- (1, 'a')
- (2, 'b')
- (3, 'c')
- >>>
- >>> print_iterator(zip(a,b,c))
- (1, 'a', 'one')
- (2, 'b', 'two')
- (3, 'c', 'three')
5.
注意 adict.keys() 返回的只是 adict 的 keys 的视图
- >>> adict = dict(a=1, b=2)
- >>> adict
- {'a': 1, 'b': 2}
- >>> view = adict.keys()
- >>> view
- dict_keys(['a', 'b'])
- >>> adict['c'] = 3
- >>> view
- dict_keys(['a', 'b', 'c'])
6.不一样的逻辑运算返回值
大概规则就是返回第一个可以判别表达式真假对象
- >>> '' and 'a' and 'b'
- ''
- >>> 'c' and '' and 'b'
- ''
- >>> 'c' and 0 and 'b'
- 0
- >>> '' or 'a' or 'b'
- 'a'
- >>> 'c' or '' or 'b'
- 'c'
- >>> '' or 0 or 'b'
- 'b'
- >>> 1 and 3 and 4
- 4
- >>> 0 or '' or []
- []
7.注意list的迭代方式,若要获得 (k, v) ,需要调用 adict.items() , 直接迭代只能获得 key, 和 adict.keys() 是完全等效的
- >>> adict = {'one':'first', 'two':'second', 'three':'third'}
- >>> adict
- {'one': 'first', 'two': 'second', 'three': 'third'}
- >>> it = iter(adict)
- >>> it
- <dict_keyiterator object at 0x010A8F60>
- >>> next(it)
- 'one'
- >>> keys = adict.keys()
- >>> keys
- dict_keys(['one', 'two', 'three'])
- >>> items = adict.items()
- >>> items
- dict_items([('one', 'first'), ('two', 'second'), ('three', 'third')])
- >>> iter(items)
- <dict_itemiterator object at 0x010BAC30>
- >>> iter(keys)
- <dict_keyiterator object at 0x010BAC90>
Python学习小记(2)---[list, iterator, and, or, zip, dict.keys]的更多相关文章
- Python学习笔记014——迭代器 Iterator
1 迭代器的定义 凡是能被next()函数调用并不断返回一个值的对象均称之为迭代器(Iterator) 2 迭代器的说明 Python中的Iterator对象表示的是一个数据流,被函数next()函数 ...
- python学习小记
python HTTP请求示例: # coding=utf-8 # more materials: http://docs.python-requests.org/zh_CN/latest/user/ ...
- Python学习小记(5)---Magic Method
具体见The Python Language Reference 与Attribute相关的有 __get__ __set__ __getattribute__ __getattr__ __setat ...
- Python学习小记(4)---class
1.名称修改机制 大概是会对形如 __parm 的成员修改为 _classname__spam 9.6. Private Variables “Private” instance variables ...
- Python学习小记(3)---scope&namespace
首先,函数里面是可以访问外部变量的 #scope.py def scope_test(): spam = 'scope_test spam' def inner_scope_test(): spam ...
- Python学习小记(1)---import小记
在这种目录结构下,import fibo会实际导入fibo文件夹这个module λ tree /F 卷 Programs 的文件夹 PATH 列表 卷序列号为 BC56-3256 D:. │ fib ...
- python 学习小记之冒泡排序
lst =[11,22,44,2,1,5,7,8,3] for i in range(len(lst)): i = 0 while i < len(lst)-1: ...
- Python学习 Day 3 字符串 编码 list tuple 循环 dict set
字符串和编码 字符 ASCII Unicode UTF-8 A 1000001 00000000 01000001 1000001 中 x 01001110 00101101 11100100 101 ...
- [Python学习]Iterator 和 Generator的学习心得
[Python学习]Iterator 和 Generator的学习心得 Iterator是迭代器的意思,它的作用是一次产生一个数据项,直到没有为止.这样在 for 循环中就可以对它进行循环处理了.那么 ...
随机推荐
- 如何清理 Docker 占用的磁盘空间
Docker 很占用空间,每当我们运行容器.拉取镜像.部署应用.构建自己的镜像时,我们的磁盘空间会被大量占用. 如果你也被这个问题所困扰,咱们就一起看一下 Docker 是如何使用磁盘空间的,以及如何 ...
- Qt Installer Framework翻译(7-1)
配置文件 配置文件可自定义安装程序的用户界面和行为.该文件通常称为config.xml,位于config文件夹中. 最小配置文件由一个<Installer>根元素组成,<Name&g ...
- 7、python基本数据类型之散列类型
前言:python的基本数据类型可以分为三类:数值类型.序列类型.散列类型,本文主要介绍散列类型. 一.散列类型 内部元素无序,不能通过下标取值 1)字典(dict):用 {} 花括号表示,每一个元素 ...
- 从零开始ming的多人联机游戏--游戏客户端(1)六边形地图
打算做的小游戏是一个多人联机的策略类游戏,类似于<文明>那种 游戏的玩法并不确定,开这个坑主要是为了入门后端开发,顺便熟悉下游戏开发 这篇文章使用unity,实现了六边形单元地图的创建.后 ...
- 超越队西柚考勤系统——beta冲刺1
这个作业属于哪个课程 http://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业的要求在哪里 https://www.cn ...
- Python学习,第四课 - 字符串相关操作
这次主要说说Python中字符串的使用方法详解 capitalize 首字母大写 print('chengshou'.capitalize()) #输出结果:Chengshou title 修改成标题 ...
- linux下误清用户/home下的文件怎么办?
2016-08-19 10:38:10 有时候我们不小心把home目录下的用户目录删除了,出现上图情况,每行开头直接变成-bash-3.2$这种形式而不是[lyp@centos7 ~]$这种,这时 ...
- (笔记)常用Llinu命令(一)
Linux资源 鸟哥Linux:http://linux.vbird.org/linux_basic/ Linux命令大全:https://man.linuxde.net/ 目录切换 cd usr: ...
- Servlet概念及配置
Servlet 简介: servlet就是sun公司开发动态web的一门技术 Sun在这些API中提供一个接口叫做:Servlet,如果逆向开发一个Servlet程序,只需要完成两个小步骤: 1.编写 ...
- Stopping service [Tomcat] Disconnected from the target VM, address:XXXXXX解决方案
原文出处:https://blog.csdn.net/u013294097/article/details/90677049 Stopping service [Tomcat] Disconnecte ...