python 列表 元祖 字典 集合_python-列表list- 元组- 集合-字典(dict)-实例代码

本文详细介绍了Python中常用的四种基础数据类型:列表(list)、元组(tuple)、集合(set)和字典(dict)的特点及使用方法。包括每种类型的定义方式、基本操作如增删改查、索引访问等,并提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[0]))                 #输出1

print('访问最后一个元素:{}'.format(m_list[-1:]))             #输出[13]返回的是list

print('访问最后一个元素:{}'.format(m_list[-1]))              #输出13返回的是元素

print('访问倒数第二个元素:{}'.format(m_list[-2]))            #输出def

print('访问2-5元素:{}'.format(m_list[2:5]))                  #输出3=(5-2)个元素,序号从0开始 不包含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('得到列表中的元素个数len(l1)={}'.format(len(l1)))  #输出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=[]

"""

"""

for i in range(11, 20):                                   # 输出0开始不包含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('访问第三个元素{}'.format(day))  #输出为30

#     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集合一样

function(){ //MT4手数 http://www.kaifx.cn/mt4/kaifx/1693.html

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值