流畅的python-2
一、python数据结构 ----------------> () [] {}
List 列表[] 可变的
- lst = [1,2,3,4]
- #改
- lst[(元素下标)] = '需要修改的' #通过下表修改
- lst[下标:下标] = '需要修改的' #通过范围修改
- #加
- lst.append() #追加
- lst.insert((插在谁的后面),(插什么)) #插入
- lst.extend() #扩展迭代添加
- #删除
- lst.remove() #移除删除指定元素
- lst.pop() #弹出默认删除最后一个 pop有返回值,返回的是被删除的元素
- lst.pop((元素下标)) #通过元素下标删除元素
- lst.clear() #清空
- del lst #删除整个列表
- del lst[(元素下标)] #通过元素下标删除元素
- #查
- print(lst[(元素下标)]) #查询
- for i in list:
print(i) #循环查询
- #其他操作
- s = lst.count(3) #统计
- s = lst.index(5) #索引print(lst)
- lst.reverse() #反转
- lst.sort() #排序
- lst.sort(reverse = True) 和 lst.sort() \n lst.reverse() 意义一样
- #列表嵌套
tuple 元祖 ()
- tu = () 括号里如果只有一个类型就是这个类型
- 不可更改
- tu.index(元素)
- tu.count(元素)
- 元祖嵌套
dict 字典
- # 键值对 {键:值}
- # 键必须是不可变的数据类型
- # 字典是可变的,因为字典是可变的
- # 值是没有要求的
dict1 = {"name":"zhangsan","age":38} # 定义一个字典
print(dict1) # 输出字典
dict2 = {"sing_dog":True} # 定义一个字典
print(dict2) print("***"*20) # 小窍门:直接答应60个*
# update()函数:将一个字典添加到另外一个字典的末尾
dict1.update(dict2)
print(dict1) # 输出字典 print("***"*20) # 小窍门:直接答应60个*
# 情况字典dict1
dict1.clear()
print(dict1) # 空字典
# 情况字典dict2
dict2.clear()
print(dict2) # 空字典
dict.get('name')
for k,v in dict:
print k,v
#字典的添加、删除、修改操作
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
dict["w"] = "watermelon"
del(dict["a"])
dict["g"] = "grapefruit"
print dict.pop("b")
print dict
dict.clear()
print dict
#字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for k in dict:
print "dict[%s] =" % k,dict[k]
#字典items()的使用
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()
#调用items()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for (k, v) in dict.items():
print "dict[%s] =" % k, v
#调用iteritems()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print dict.iteritems()
for k, v in dict.iteritems():
print "dict[%s] =" % k, v
for (k, v) in zip(dict.iterkeys(), dict.itervalues()):
print "dict[%s] =" % k, v
#使用列表、字典作为字典的值
dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}
print dict["a"]
print dict["a"][0]
print dict["bo"]
print dict["bo"]["o"]
print dict["g"]
print dict["g"][1]
p, li { white-space: pre-wrap }
去重復:
a=[1,2,3,3,4,4,5,6]
b=list(set(a))
''.join()是字符串操作函数,常常用于字符连接操
a=“abcd”
“,”.join(a)
‘a,b,c,d’ “|”.join([‘a’,‘b’,‘c’])
‘a|b|c’
流畅的python-2的更多相关文章
- 《流畅的python》读书笔记
流畅的python 第1章 python数据模型 ---1.1 一摞Python风格的纸牌 特殊方法,即__method__,又被称为魔术方法(magic method)或者双下方法(dunder-m ...
- 流畅的python(笔记)
流畅的python中有很多奇技淫巧,整本书都在强调如何最大限度地利用Python 标准库.介绍了很多python的不常用的数据类型.操作.库等,对于入门python后想要提升对python的认识应该有 ...
- 流畅的python 对象引用 可变性和垃圾回收
对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...
- 《流畅的Python》一副扑克牌中的难点
1.现在在看<流畅的Python>这本书,看了三页就发现,这本书果然不是让新手来入门的,一些很常见的知识点能被这个作者玩出花来, 唉,我就在想,下面要分析的这些的代码,就算我费劲巴拉的看懂 ...
- [读书笔记]流畅的Python(Fluent Python)
<流畅的Python>这本书是图灵科技翻译出版的一本书,作者Luciano Ramalho. 作者从Python的特性角度出发,以Python的数据模型和特殊方法为主线,主要介绍了pyth ...
- 《流畅的Python》Object References, Mutability, and Recycling--第8章
Object References, Mutability, and Recycling 本章章节: Variables Are Not Boxes identity , Equality , Al ...
- 《流畅的Python》 第一部分 序章 【数据模型】
流畅的Python 致Marta,用我全心全意的爱 第一部分 序幕 第一章 Python数据模型 特殊方法 定义: Python解释器碰到特殊句法时,使用特殊方法激活对象的基本操作,例如python语 ...
- SyntaxError: Non-UTF-8 code starting with '\xbb' in file D:\流畅学python\ex32.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
1. 报错如下: SyntaxError: Non-UTF-8 code starting with '\xd3' in file D:\流畅学python\ex34.py on line 4, bu ...
- 流畅的python学习笔记:第三章
字典的变种: OrderedDict 首先来看下面的代码,在一个字典中,有name,age,city,在遍历这个字典的时候.顺序却是随机的,不是按照我们添加的顺序也就是name->age-> ...
- 流畅的python学习笔记:第二章
第二章开始介绍了列表这种数据结构,这个在python是经常用到的结构 列表的推导,将一个字符串编程一个列表,有下面的2种方法.其中第二种方法更简洁.可读性也比第一种要好 str='abc' strin ...
随机推荐
- vue3.0 props
.orange { color: rgba(255, 165, 0, 1) } Vue3.0 props 1.你是否遇到了,引用props数据报错的问题? 在Vue3.0中,采用了proxy,让很多数 ...
- Docker系列——Grafana+Prometheus+Node-exporter钉钉推送(四)
近期搭建的服务器监控平台,来进行一个总结.主要分为监控平台的搭建.告警中心的配置以及消息的推送.推送的话,支持多种终端.具体详细可查看之前的博文,在这里罗列下,方便查看. Docker系列--Graf ...
- 『无为则无心』Python基础 — 14、Python流程控制语句(while循环语句)
目录 1.什么是循环结构 2.while循环 (1)while循环的语法 (2)while循环的应用 3.while语句的死循环 4.break和continue 5.while循环嵌套 (1)应用场 ...
- Redis的bitmap从基础到业务
1. 位与字节 1个字节(byte)等于8个位(bit).(计算机常识). 2. string与bitmap Redis里的bitmap是属于string这个数据类型里的.可以help进行查看bit相 ...
- Redisson 分布式锁实现之源码篇 → 为什么推荐用 Redisson 客户端
开心一刻 一男人站在楼顶准备跳楼,楼下有个劝解员拿个喇叭准备劝解 劝解员:兄弟,别跳 跳楼人:我不想活了 劝解员:你想想你媳妇 跳楼人:媳妇跟人跑了 劝解员:你还有兄弟 跳楼人:就是跟我兄弟跑的 劝解 ...
- 13 Nginx访问日志分析
#!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin # Nginx 日志格式: # ...
- 12、elk的使用(2)
12.8.收集日志: 因为logstash安装在从节点上,所以这里收集的主要是从节点上的服务日志: 1.收集系统日志: (1)配置文件: vim /etc/logstash/conf.d/system ...
- 8、mysql乱码问题及字符集实战
8.1.mysql插入中文数据乱码案例: mysql建库的字符集为latin1,客户端的字符集为utf8; use lc; 1.查看库的编码: mysql> show create databa ...
- SpringBoot Redis 2.0.x
redis的安装 在笔者之前的文章中有介绍redis的安装,不会的可以去看 笔者之前写的文章redis安装 完成安装后如果不熟悉redis的操作,redis官方文档也有基本操作指南,redis基本操作 ...
- CentOS-yum安装Nginx
查看系统版本 $ cat /etc/redhat-release Nginx 不在默认的 yum 源中,使用官网的 yum 源 $ rpm -ivh http://nginx.org/packages ...