python-列表list- 元组(tuple)- 集合(set)-字典(dict)-实例代码
python 经常用的数据类型还有列表list- 元组(tuple)- 集合(set)-字典(dict),以下是这四种类型用法的简单示例。
1.列表list [ ] 元素可变 , 有序
2. 元组(tuple) ( ) 元素不可变,可访问
3.集合 set { } 元素不可重复,没有索引和位置的概念, 无序
4. 字典dict { } key:value 无序
Python 中组合数据类型-列表list- 元组(tuple)- 字典(di)。
1.list [ ]
list 有序元素集合 元素类型可不同
1.访问 单个访问 l=list[0] l=[1,3,5,7,9,11,13]
多个访问l[2:5]
2.列表操作
list1+list2 合并连接两个列表
list1*n 重复n次列表内容
len(list1) 返回列表长度(元素个数)
x in list1 检查元素是否在列表中
l1*l2 错
3.长度 len(list1) len(l1)=6
4. 检查元素是否在列表中 x in l1
"""
def main():
# m_list=[1,3,5,'abc',9,'def',13] #赋值为什么就是什么数据类型 也可以写成 m_list=[]
m_list=[1,3,5,7,9,11,13] #赋值为什么就是什么数据类型 也可以写成 m_list=[]
print(m_list) #输出[1,3,5,7,9,11,13]
print('访问最后一个元素:{}'.format(m_list[-1:])) #输出[13] 返回的是list
print('访问最后一个元素:{}'.format(m_list[-1])) # 输出13 返回的是元素
print('访问倒数第二个元素:{}'.format(m_list[-2])) # 输出def
开始 不包含5,输出[5,'abc',9]
print('不访问最后两个元素:{}'.format(m_list[:-2])) # [1, 3, 5, 7, 9]
"""
"""
l1=[1,2,3]
l2=[4,5,6]
print('加相当于合并 l1+l2={}'.format(l1+l2)) #输出[1,2,3,4,5]
print('乘相当于重复 l1*2={}'.format(l1*2)) # 输出[1,2,3,1,2,3]
print('元素是否在列表中 {}'.format(10 in l1)) #输出 False
print('元素是否在列表中 {}'.format('abc' in l1)) # 输出 False
print('元素是否在列表中 {}'.format(5 in l2)) # 输出 True
i=0
m_list=[]
while i <= 10:
m_list.append(i)
i+=1
print('while m_list中的所有元素{}'.format(m_list)) # 输出[0,1,2,3,4,5,6,7,8,9,10]
for i in range(0, 6): #Pop i in range(0,5)
m_list.pop(i) #remove i in range(0,10)
# m_list.remove(i)
print('pop操作i={},m_list={}'.format(i,m_list))
"""
"""
pop操作i = 0,m_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
pop操作i = 1,m_list = [1, 3, 4, 5, 6, 7, 8, 9, 10]
pop操作i = 2,m_list = [1, 3, 5, 6, 7, 8, 9, 10]
pop操作i = 3,m_list = [1, 3, 5, 7, 8, 9, 10]
pop操作i = 4,m_list = [1, 3, 5, 7, 9, 10]
pop操作i=5,m_list=[1, 3, 5, 7, 9]
"""
"""
remove操作i=0,m_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
remove操作i=1,m_list=[2, 3, 4, 5, 6, 7, 8, 9, 10]
remove操作i=2,m_list=[3, 4, 5, 6, 7, 8, 9, 10]
remove操作i=3,m_list=[4, 5, 6, 7, 8, 9, 10]
remove操作i=4,m_list=[5, 6, 7, 8, 9, 10]
remove操作i=5,m_list=[6, 7, 8, 9, 10]
remove操作i=6,m_list=[7, 8, 9, 10]
remove操作i=7,m_list=[8, 9, 10]
remove操作i=8,m_list=[9, 10]
remove操作i=9,m_list=[10]
remove操作i=10,m_list=[]
"""
"""
开始不包含10
m_list.append(i)
print('for m_list中的所有元素{}'.format(m_list)) #
"""
2.元组 tuple()
1.元组是结构 列表是顺序
2.元组由不同数据组成 列表由相同数据组成
3.一旦被创建 元素不能被修改
4.('red','blue','green'),(2,4,6)
5.与list访问相同
6.表达固定数据项、函数多返回值
# def main():
# days_tup = (31, 'abc', 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# day=days_tup[:3]
# print('访问前三个元素{}'.format(day)) #输出为(31 'abc' 31)
# print(type(day)) #返回的是元组
# day = days_tup[2]
# print(type(day)) #返回的是int
3.集合 set {}
1.无序组合
2.元素不可重复
3.没有索引和位置的概念
4.set()用于集合的生成,返回结果是一个无重复且排序任意的集合
5.用于表示成员间的关系、元素去重
def main():
days_set1 = {1,2,3,4,5}
days_set2 = {4,5,7,8,9,10,}
i=0
for i in range(i,12):
if i in days_set1:
print('i={}在集合days_set1中'.format(i))
elif i in days_set2:
print('i={}在集合days_set2中'.format(i))
elif i in days_set1-days_set2:
print('i={}在days_set1中但不在days_set2中'.format(i))
elif i in days_set1 & days_set2:
print('i={}在days_set1和days_set2交集'.format(i))
elif i in days_set1 | days_set2:
print('i={}在days_set1 days_set2所有元素'.format(i))
else:
print('i={}不在集合两个集合中'.format(i))
4.字典 增删改查
遍历所有的key for key in d.keys():
Print(key)
遍历所有的value for value in d.values():
Print(value)
遍历所有的数据项 for item in d.items():
Print(items)
dict 键-值
字典类型数据通过映射查找数据项
映射: 任意键查找集合中的值的过程
一个键对应一个值
字典是无序的。
# 1.创建变量 增删改查
#2.是否在字典中
d=dict() #定义一个空变量
print(d)
d['egg']=2.12 #赋值 输出{'egg': 2.12}
print(d)
d['milk']=3.15 #增加 输出{'egg': 2.12, 'milk': 3.15}
print(d)
#['1'] 和[1] 不同
d['1']=3.15 #{'egg': 2.12, 'milk': 3.15, '1': 3.15}
print(d)
d[1] = 3.15 #{'egg': 2.12, 'milk': 3.15, '1': 3.15, 1: 3.15}
print(d)
d['egg'] = 5.15 #修改 {'egg': 5.15, 'milk': 3.15}
print(d)
del d['egg'] #删除 {'milk': 3.15}
print(d)
b_in = 'milk' in d #是否在外汇返佣中 True 集合一样
print(b_in)
def main():
# 月份-天数 字典
month_day_dict = {1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31}
day_month_dict = {30: {4, 6, 9, 11},
31: {1, 3, 5, 7, 8, 10, 12}}
for month_day_key in month_day_dict.keys():
print("month_day_dict中的key值遍历{}".format(month_day_key))
for month_day_value in month_day_dict.values():
print("month_day_dict中的values值遍历{}".format(month_day_value))
for item in day_month_dict.items():
print("day_month_dict中的item{}".format(item))
print (type(item))
Pass
原文链接:https://blog.csdn.net/aggie4628/article/details/103431800
python-列表list- 元组(tuple)- 集合(set)-字典(dict)-实例代码的更多相关文章
- python 数据类型: 字符串String / 列表List / 元组Tuple / 集合Set / 字典Dictionary
#python中标准数据类型 字符串String 列表List 元组Tuple 集合Set 字典Dictionary 铭记:变量无类型,对象有类型 #单个变量赋值 countn00 = '; #整数 ...
- <转>python列表、元组、集合、字典、json相互转换以及其他基础入门
列表元组转其他 # 列表转集合(去重) list1 = [6, 7, 7, 8, 8, 9] set(list1) # {6, 7, 8, 9} #两个列表转字典 list1 = ['key1','k ...
- day8 python 列表,元组,集合,字典的操作及方法 和 深浅拷贝
2.2 list的方法 # 增 list.append() # 追加 list.insert() # 指定索引前增加 list.extend() # 迭代追加(可迭代对象,打散追加) # 删 list ...
- Python 基础补充(一) 列表、元组、集合、字典的区别和相互转换
一.列表.元组.集合.字典的区别 列表 元组 集合 字典 英文 list tuple set dict 可否读写 读写 只读 读写 读写 可否重复 是 是 否 是 存储方式 值 值 键(不能重复) ...
- 5. Python数据类型之元组、集合、字典
元组(tuple) 元组创建很简单,只需要在小括号中添加元素,并使用逗号隔开即可.与列表不同的是,元组的元素不能修改.如下代码所示: tup1 = () tup2 = (1) tup3 = (1,) ...
- Python中列表、元组、集合、字典
Python 列表(List) 列表是最常用的Python数据类型: 列表中的数据项不需要具有相同的类型: 列表也叫做数组,定义时使用[]: 通过下标访问列表中的元素,下标从 0 开始计数 列表的增 ...
- 《Python高性能编程》——列表、元组、集合、字典特性及创建过程
这里的内容仅仅是本人阅读<Python高性能编程>后总结的一些知识,用于自己更好的了解Python机制.本人现在并不从事计算密集型工作:人工智能.数据分析等.仅仅只是出于好奇而去阅读这本书 ...
- Python中字符串、列表、元组、集合、字典中的一些知识,有些不太常见
————————笔记——————————# 字符串1. 字符串是不可变的.2. 字符串切片输出:`[start:end:step]`.使用`a[::-1]`倒序输出字符串.3. `str.split( ...
- python -- 结构数据类型(列表、元组、集合、字典)
一.列表 列表表示一组有序的元素,这些元素可以是数字.字符串,也可以是另一个列表. # ----------------------------------------# 列表(list):一组有序的 ...
- python3_列表、元组、集合、字典
列表list #列表的基本操作 >>> a=[] #创建空列表 >>> a = [0,1,2,3,4,5] #创建列表并初始化,列表是[]包含由逗号分隔的多个元素组 ...
随机推荐
- SOA架构是什么?
https://blog.csdn.net/u013343616/article/details/79460398 SOA是什么?SOA全英文是Service-Oriented Architectur ...
- 如何使用WidsMob Montage—蒙太奇制作有趣的动物照片?
今天,越来越多的人有宠物.根据最近的一项调查,超过六成的美国人在家中至少有一只宠物.这些宠物不是动物,而是家庭成员.因此,有趣的动物照片成为社交媒体上的热门话题是有道理的.当您打开朋友圈或短视频APP ...
- JMeter之Beanshell用法
1. 获取JMeter脚本中的变量 ${demoParam} String demo = vars.get("demoParam"); 2. 将数据保存到JMeter脚本中的变量 ...
- python环境变量
下载并升级更新pip python -m pip install -U pip 变量名:PY_HOME 变量值:python路径 path:win10加在最后(记得用;号隔开):win7加在前面记 ...
- git 切换分支开发并合并提交到远程仓库
- C#正则表达式将html代码中的所有img标签提取
/// <summary> /// 取得HTML中所有图片的 URL. /// </summary> /// <param name="sHtmlText&qu ...
- socket | tcp客户端 tcp服务器 udp客户端 udp 服务器 创建方法
tcp服务器 #coding=utf-8 ''' 这里是tcp服务器端,要先启动 ''' import socket import threading bind_ip = "0.0.0.0& ...
- SyntaxError: missing ] after element list
在前端页面js报错,找了很久没找到原因. 后来发现是后台向前端输出json字符串,而前端接收是html格式,需要将后台json字符串改成正常字符串就可以输出,或者通过ajax的方式接收json字符串.
- VS2010提示error TRK0002: Failed to execute command
转自VC错误:http://www.vcerror.com/?p=277 问题描述: windows8自动更新Microsoft .NET Framework 3.5和4.5.1安全更新程序,今天用V ...
- MySQL-初始化和自动更新TIMESTAMP和DATETIME
https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html 例,添加自动更新的保存最后一次修改该条记录的时间戳的字段: ...