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)-实例代码的更多相关文章

  1. python 数据类型: 字符串String / 列表List / 元组Tuple / 集合Set / 字典Dictionary

    #python中标准数据类型 字符串String 列表List 元组Tuple 集合Set 字典Dictionary 铭记:变量无类型,对象有类型 #单个变量赋值 countn00 = '; #整数 ...

  2. <转>python列表、元组、集合、字典、json相互转换以及其他基础入门

    列表元组转其他 # 列表转集合(去重) list1 = [6, 7, 7, 8, 8, 9] set(list1) # {6, 7, 8, 9} #两个列表转字典 list1 = ['key1','k ...

  3. day8 python 列表,元组,集合,字典的操作及方法 和 深浅拷贝

    2.2 list的方法 # 增 list.append() # 追加 list.insert() # 指定索引前增加 list.extend() # 迭代追加(可迭代对象,打散追加) # 删 list ...

  4. Python 基础补充(一) 列表、元组、集合、字典的区别和相互转换

    一.列表.元组.集合.字典的区别   列表 元组 集合 字典 英文 list tuple set dict 可否读写 读写 只读 读写 读写 可否重复 是 是 否 是 存储方式 值 值 键(不能重复) ...

  5. 5. Python数据类型之元组、集合、字典

    元组(tuple) 元组创建很简单,只需要在小括号中添加元素,并使用逗号隔开即可.与列表不同的是,元组的元素不能修改.如下代码所示: tup1 = () tup2 = (1) tup3 = (1,) ...

  6. Python中列表、元组、集合、字典

    Python 列表(List) 列表是最常用的Python数据类型: 列表中的数据项不需要具有相同的类型: 列表也叫做数组,定义时使用[]: 通过下标访问列表中的元素,下标从 0  开始计数 列表的增 ...

  7. 《Python高性能编程》——列表、元组、集合、字典特性及创建过程

    这里的内容仅仅是本人阅读<Python高性能编程>后总结的一些知识,用于自己更好的了解Python机制.本人现在并不从事计算密集型工作:人工智能.数据分析等.仅仅只是出于好奇而去阅读这本书 ...

  8. Python中字符串、列表、元组、集合、字典中的一些知识,有些不太常见

    ————————笔记——————————# 字符串1. 字符串是不可变的.2. 字符串切片输出:`[start:end:step]`.使用`a[::-1]`倒序输出字符串.3. `str.split( ...

  9. python -- 结构数据类型(列表、元组、集合、字典)

    一.列表 列表表示一组有序的元素,这些元素可以是数字.字符串,也可以是另一个列表. # ----------------------------------------# 列表(list):一组有序的 ...

  10. python3_列表、元组、集合、字典

    列表list #列表的基本操作 >>> a=[] #创建空列表 >>> a = [0,1,2,3,4,5] #创建列表并初始化,列表是[]包含由逗号分隔的多个元素组 ...

随机推荐

  1. 阿里云基于OSS的云上统一数据保护方案2.0技术解析

    近年来,随着越来越多的企业从传统经济向数字经济转型,云已经渐渐成为数据经济IT新常态.核心业务系统上云,云上的业务创新,这些都产生了大量的业务数据,这些数据也成为了企业最重要的资产.资源. 阿里云基于 ...

  2. 【HDOJ6602】Longest Subarray(线段树,vector)

    题意:给定一个长为n的序列,第i个数a[i]都是一个[1,c]中的整数 如果一段序列[l,r]中出现过的数字出现次数都>=K则称其为好的序列 求最长的好的序列的长度 n,k,c,a[i]< ...

  3. react教程 — 性能优化

    参考:https://segmentfault.com/a/1190000007811296?utm_medium=referral&utm_source=tuicool  或  https: ...

  4. ansible控制winserver笔记

    原文地址: https://www.cnblogs.com/kingleft/p/6391652.html 环境描述: ansible控制远程windows .系统必须是sp1 .安装framewor ...

  5. 2018-2019-2 实验三 敏捷开发与XP实践

    实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实验.撰写 ...

  6. get the deadlock information from sql server

    https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...

  7. 20150709---Web中GridView控件根据绑定的数据显示不同的图片

    示例图: 根据数据判断,显示各种不同的图片 该列的前端代码: <asp:TemplateField HeaderText="审图"> <ItemTemplate& ...

  8. 使用Excel表格的记录单功能轻松处理工作表中数据的方法

    使用Excel表格的记录单功能轻松处理工作表中数据的方法 记录单是将一条记录分别存储在同一行的几个单元格中,在同一列中分别存储所有记录的相似信息段.使用记录单功能可以轻松地对工作表中的数据进行查看.查 ...

  9. python 装饰器 第八步:使用类来作为装饰器参数

    #第八步:使用类作为装饰器参数 #装饰器使用的操作类 class Wish: #祈求方法 def before(): print('饭前洗洗手') #还愿方法 def after(): print(' ...

  10. 对象和数据库的天然阻抗 转摘于:http://www.jdon.com/mda/oo-reltaion2.html

    在“面向对象建模与数据库建模两种分析设计方法的比较”一文中我们比较了在对需求分析时两种方法的不同,所谓数据库建模分析,就是项目一开始就根据需求建立数据库模型,如数据表结构和字段等,这种错误现象大量普遍 ...