python 列表/元组/字典
来源:互联网 发布:趣店入职配mac 编辑:程序博客网 时间:2024/06/03 01:30
1.Python列表
List(列表) 是 Python 中使用最频繁的数据类型。2.Python元组以下是元组无效的,因为元组是不允许更新的。而列表是允许更新的:
3.Python元字典
List(列表) 是 Python 中使用最频繁的数据类型。
列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。
列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。
列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。
加号(+)是列表连接运算符,星号(*)是重复操作。如下实例:
点击(此处)折叠或打开
- #!/usr/bin/python
- # -*- coding: UTF-8-*-
- list = ['abcd', 786, 2.23,'john', 70.2]
- tinylist = [123,'john']
- print list # 输出完整列表
- print list[0] # 输出列表的第一个元素
- print list[1:3] # 输出第二个至第三个的元素
- print list[2:] # 输出从第三个开始至列表末尾的所有元素
- print tinylist * 2 # 输出列表两次
- print list + tinylist # 打印组合的列表
元组是另一个数据类型,类似于List(列表)。
元组用"()"标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
点击(此处)折叠或打开
- #!/usr/bin/python
- # -*- coding: UTF-8-*-
- tuple = ('abcd', 786, 2.23,'john', 70.2)
- tinytuple = (123,'john')
- print tuple # 输出完整元组
- print tuple[0] # 输出元组的第一个元素
- print tuple[1:3] # 输出第二个至第三个的元素
- print tuple[2:] # 输出从第三个开始至列表末尾的所有元素
- print tinytuple * 2 # 输出元组两次
- print tuple + tinytuple # 打印组合的元组
点击(此处)折叠或打开
- #!/usr/bin/python
- # -*- coding: UTF-8-*-
- tuple = ('abcd', 786, 2.23,'john', 70.2)
- list = ['abcd', 786, 2.23,'john', 70.2]
- tuple[2]= 1000 # 元组中是非法应用
- list[2]= 1000 # 列表中是合法应用
字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
点击(此处)折叠或打开
- #!/usr/bin/python
- # -*- coding: UTF-8-*-
- dict = {}
- dict['one']= "This is one"
- dict[2]= "This is two"
- tinydict = {'name':'john','code':6734,'dept': 'sales'}
- print dict['one'] # 输出键为'one' 的值
- print dict[2] # 输出键为 2 的值
- print tinydict # 输出完整的字典
- print tinydict.keys() # 输出所有键
- print tinydict.values() # 输出所有值
0 0
- Python:元组、列表、字典
- python字典,列表,元组
- python 元组,列表,字典
- python 字典,列表,元组
- python 列表/元组/字典
- python--列表 、元组、字典
- python 列表、元组、字典
- python 列表,元组,字典
- Python 列表 元组 字典
- python列表元组字典
- Python 元组、列表、字典、文件
- python的元组、列表、字典
- Python 元组、列表、字典、文件
- python列表、元组、字典、字符串操作
- Python 元组、列表、字典、文件
- Python 元组+列表+字典+文件
- Python 元组、列表、字典、文件
- Python 元组、列表、字典、文件
- find the install path of some software in linux (ubuntu)
- manage .deb package in linux (ubuntu) dpkg
- some package command in linux (ubuntu) zip/ tar/ tar.gz/ tar.bz2
- Python 字典(Dictionary) setdefault()方法
- the shutdown command in linux for shutdown in time or reboot
- python 列表/元组/字典
- python 单行注释/多行注释/代码间注释/特殊注释/"""doc String
- 深入理解python之self
- install pysqlite in ubuntu
- ubuntu mentohust solutions summary
- error: sqlite3.h: No such file or directory #include "sqlite3.h"
- python中的接口
- set() in python
- 深入理解Python的字符编码utf-8 & unicode