python 变量、列表、元组、字典
python 变量、列表、元组、字典
1、python 变量赋值
2、ptython 列表
3、python 元组
4、python 字典
1、 Python变量赋值
1.1变量的命名规则
变量名只能是 字母、数字或下划线的任意组合
变量名的第一个字符不能是数字
以下关键字不能声明为变量名
[ 'assert','and', 'as', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
1.2变量赋值的三种方式
传统赋值:student = “kezi”
链式赋值: student= user = “kezi”
序列解包赋值: stuent,age = “kezi”,10
>>> student="kezi"
>>> print(student)
kezi
>>> student2=student
>>> print(student2)
kezi
>>> student="keke"
>>> print (student2)
kezi
>>> print(student)
keke
2、 python列表
列表是我们最常用的数据类型之一,是一个元素以逗号分割,以中括号包围的,有序的,可修改、存储、的序列。
例子:
>>> student=["zhangshan","liuliu","taotao","junjun","xixi"]
>>> student[1:4]
['liuliu', 'taotao', 'junjun']
>>> student[2:-1]
['taotao', 'junjun']
>>> student[2:-1]
['taotao', 'junjun']
>>> student[2:]
['taotao', 'junjun', 'xixi']
>>> student[0:2:]
['zhangshan', 'liuliu']
>>>
2.2列表的方法
|
列表的添加 |
append |
追加,在列表的尾部加入指定的元素 |
|
extend |
将指定序列的元素依次追加到列表的尾部 |
|
|
insert |
将指定的元素插入到对应的索引位上,注意负索引 |
|
|
列表的查找 注 列表没有find方法 |
count |
计数,返回要计数的元素在列表当中的个数 |
|
index |
查找,从左往右返回查找到的第一个指定元素的索引,如果没有找到,报错 |
|
|
列表的排序 |
reverse |
索引顺序倒序 |
|
sort |
按照ascii码表顺序进行排序 |
|
|
列表的删除
|
pop |
弹出,返回并删除指定索引位上的数据,默认-1 |
|
remove |
从左往右删除一个指定的元素 |
|
|
del |
删除是python内置功能,不是列表独有的 |
例子:
列表的添加
>>> student
['zhangshan', 'liuliu', 'taotao', 'junjun', 'xixi']
>>> student.insert(2,"honghonghong") #默认是从下标是0开始的,0,1,2,3,4,,,,
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi'] >>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi']
>>> student.append("haihaihai")
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai']
>>> student.extend("kaikaikai")
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'k', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']
统计、查找
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'k', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']
>>> student.count("k")
3
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
>>> print (student.index("honghonghong"))
2
删除
>>> student.remove("k")
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']
>>> student.pop()
'i'
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'i', 'k', 'a', 'i', 'k', 'a']
>>> del student[8]
>>> student
['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
注:列表中可以建立子列表。
浅copy
import copy
student=['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
student1=copy.copy(student) print (student)
print(student1)
student[2][0]="kaikaikai"
print (student)
print(student1)
打印结果:
['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['kaikaikai', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['kaikaikai', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
为什么改 student ,student1也在变呢,由于student与student1的第二层内存地址是指向了同一个内存地址。
深copy
import copy
student3=['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
student4=copy.deepcopy(student3)
print (student3)
print(student4)
student3[2][0]="xinxinxin"
print (student3)
print(student4)
student4[2][0]="yunyunyun"
print (student3)
print(student4)
打印结果
['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['xinxinxin', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['xinxinxin', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
['zhangshan', 'liuliu', ['yunyunyun', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
深 copy 存放地址不在同一个地方了,因此打印结果不一样。
3、python 元组
元组是元素以逗号分割,以小括号包围的有序的,不可修改的序列。
元组的特性:
(1)元组可以不加括号
(2)单元素元组需要加逗号
(3)元组不可修改,所以我们在配置文件当中多看到元组
元组的方法
|
元组的查找 |
index |
从左往右返回第一个遇到的指定元素的索引,如果没有,报错 |
|
count |
返回元组当中指定元素的个数 |
元组和字符串的区别
(1)、 元组和字符串都是有序的,不可修改的序列
(2)、 元组的元素可以是任何类型,字符串的元素只能是字符
(3)、 元组的元素长度可以任意,字符串的元素长度只能为1
4、python 字典
字典一种key - value 的数据类型
字典一个元素呈键值对的形式,以逗号分割,以大括号包围的无序的,可以修改的序列。
字典是python基础数据类型当中唯一一个映射关系的数据类型
字典的特点:
因为字典是无序的,所以字典没有索引值,
因为字典没有索引值,所以字典以键取值,(字典的键相当于列表的索引)
字典的方法:
|
字典的取值 |
keys |
获取字典所有的键 |
|
values |
获取字典所有的值 |
|
|
get |
以键取值,如果指定键不存在,默认返回None,可以指定返回内容 |
|
|
update |
更新指定键的内容,如果键不存在,创建 |
|
|
setdefault |
设置默认,如果键存在,返回值,如果键不存在,创造键,值默认为None,值也可以自定义 |
|
|
items |
返回字典键值呈元组形式的格式 |
|
|
字典的删除 |
pop |
弹出,返回并删除指定键对应的值 |
|
popitem |
随机弹出一个键值元组,这里随机的原因是因为字典无序 |
|
|
clear |
清空字典 |
info={
"s001":"zhangsan" ,
"s002":"lisi",
"s003":"wangwu"
}
print(info)
打印结果
{'s003': 'wangwu', 's001': 'zhangsan', 's002': 'lisi'}
增加
info['s004']="wangmazi"
print(info)
打印结果
{'s004': 'wangmazi', 's002': 'lisi', 's001': 'zhangsan', 's003': 'wangwu'}
修改
info['s001']="huangda"
print(info)
打印结果
{'s003': 'wangwu', 's004': 'wangmazi', 's001': 'huangda', 's002': 'lisi'}
删除
info.pop("s004")
print(info)
打印结果
{'s001': 'huangda', 's002': 'lisi', 's003': 'wangwu'}
del info["s001"]
print(info)
打印结果
{'s002': 'lisi', 's003': 'wangwu'}
随机删除
info.popitem() #随机删除
print(info)
打印结果
{'s003': 'wangwu'}
查找
info={
"s001":"zhangsan" ,
"s002":"lisi",
"s003":"wangwu"
}
print(info)
print (info['s002'])
print(info["s004"])没有查到,要报错
print(info.get('s001'))如果没有查到,也不会报错
print("s003" in info) 标准用法
打印结果
lisi
print(info["s004"])
KeyError: 's004' 没有找到,报错
zhangsan
True
多级字典嵌套及操作
ddress1={"四川":["成都",'锦阳']
,"广东":["广州","佛山"]
,"湖南":["长沙","益阳"]
}
print(address1["四川"][0]) 查找
address1["四川"][1]="德阳" #修改
print(address1)
打印结果
成都
{'四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳']}
print(address1.values())
print(address1.keys())
address1.setdefault("直辖市","上海")
print(address1) #update
qita={"特区":"香港","福建":"厦门"}
address1.update(qita)
print(address1)
打印结果
dict_values([['成都', '德阳'], ['广州', '佛山'], ['长沙', '益阳']])
dict_keys(['四川', '广东', '湖南'])
{'四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳'], '直辖市': '上海'}
{'直辖市': '上海', '特区': '香港', '四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳'], '福建': '厦门'} print(address1.items())#转化为列表
打印结果
dict_items([('福建', '厦门'), ('特区', '香港'), ('直辖市', '上海'), ('广东', ['广州', '佛山']), ('四川', ['成都', '德阳']), ('湖南', ['长沙', '益阳'])]) bb=address1.fromkeys([6,7,9],"uuuu")
print(bb) cc=dict.fromkeys([1,2,3],[1,{"name":"kezi"},555])全部都修改了
print(cc) 打印结果
{9: 'uuuu', 6: 'uuuu', 7: 'uuuu'}
{1: [1, {'name': 'kezi'}, 555], 2: [1, {'name': 'kezi'}, 555], 3: [1, {'name': 'kezi'}, 555]} 循环
for i in address1:
print(i,address1[i])
打印结果
广东 ['广州', '佛山']
特区 香港
四川 ['成都', '德阳']
直辖市 上海
福建 厦门
湖南 ['长沙', '益阳'] for i in address1:
print(i) 打印结果 广东
特区
四川
直辖市
福建
湖南
数据类型的总结
|
|
str |
list |
tuple |
dict |
|
是否有序 |
是 |
是 |
是 |
否 |
|
是否可修改 |
不 |
可 |
不 |
可 |
|
方法多少 |
很多 |
一般 |
很少 |
较多 映射关系 |
python 变量、列表、元组、字典的更多相关文章
- **python中列表 元组 字典 集合
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. 1.列表 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔. 列表的特 ...
- python中列表 元组 字典 集合的区别
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...
- Python之列表&元组&字典
今天学习了Python的基本数据类型,做以下笔记,以备查用. 一.列表 列表的常用方法: 1.append()方法 def append(self, p_object): # real signatu ...
- Python学习-列表元组字典操作
一.列表 列表是Python的基本数据类型之一,它是以 [] 括起来的,内部成员用逗号隔开.里面可以存放各种数据类型. # 例如: list2 = ['jason', 2, (1, 3), ['war ...
- python字符串/列表/元组/字典之间的相互转换(5)
一.字符串str与列表list 1.字符串转列表 字符串转为列表list,可以使用str.split()方法,split方法是在字符串中对指定字符进行切片,并返回一个列表,示例代码如下: # !usr ...
- python 中列表 元组 字典 集合的区别
先看图片解释 (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复 ...
- python字符串 列表 元组 字典相关操作函数总结
1.字符串操作函数 find 在字符串中查找子串,找到首次出现的位置,返回下标,找不到返回-1 rfind 从右边查找 join 连接字符串数组 replace 用指定内容替换指定内容,可以指定次数 ...
- python基础-列表元组字典
1.列表和元组 列表可以对数据实现最方便的存储.修改等操作 names=["Alex","tenglan","Eric","Rai ...
- 【python】列表&&元组&&字典
列表:用“[]”包裹,可对值增删改. 列表遍历: 方法一: alist=["a","b","c","d","e ...
- python之列表/元组/字典/字符串
一.列表 格式:list = ['xxx','xxx','xxx'] 性质:可以修改列表内容 copy用法: import copy names = ['] names01 = names #直接引用 ...
随机推荐
- [LeetCode]-DataBase-Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- java生成二维码学习笔记
纠错等级: QRErrorCorrectLevel.L 7%的字码可被修正 QRErrorCorrectLevel.M 15%的字码可被修正 QRErrorCorrectLevel.Q 25%的字码可 ...
- NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)
NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码) 七月,酷暑难耐,认识的几位同学参加知乎看山杯,均取得不错的排名.当时天池AI医疗大赛初赛结束,官方正在为复赛进行平台调 ...
- VLC2.2.4命令参数
用法: vlc [选项] [流] ...您可以在命令行中指定多个流.它们将被加入播放列表队列.指定的首个项目将被首先播放. 选项样式: --选项 用于设置程序执行期间的全局选项. -选项 单字母版本的 ...
- 用Vue来实现音乐播放器(三十八):歌词滚动列表的问题
1.频繁切换歌曲时,歌词会跳来跳去 原因: // 歌词跳跃是因为内部有一个currentLyric对像内部有一些功能来完成歌词的跳跃 //每个currentLyric能实现歌曲的播放跳到相应的位置 是 ...
- 用Vue来实现音乐播放器(二十三):音乐列表
当我们将音乐列表往上滑的时候 我们上面的歌手图片部分也会变小 当我们将音乐列表向下拉的时候 我们的图片会放大 当我们将音乐列表向上滑的时候 我们的图片有一个高斯模糊的效果 并且随着我们的列 ...
- 阶段3 1.Mybatis_10.JNDI扩展知识_3 补充-测试JNDI数据源的使用以及使用细节
在webapp文件夹下新建目录META-INF 把context.xml文件复制过去. 拿资料里面的SqlMapConfig.xml文件 全部复制到项目的SqlMapConfig.xml里面来. ja ...
- C#采集:图灵机器人信息
Dictionary<string, string> users = new Dictionary<string, string>(); users.Add("use ...
- mysql安装及相关配置
安装下载 第一种 安装mysql安装包 //www.jb51.net/softs/451120.html 保存root密码 打开系统偏好设置,start mysql server #配置mysql e ...
- 【ABAP系列】SAP ABAP MRKO增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP MRKO增强 ...