新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

python-列表list-元组-集合-字典(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[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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:python-列表list-元组-集合-字典(dict)-实例代码-创新互联
文章网址:http://www.wjwzjz.com/article/cseigh.html
在线咨询
服务热线
服务热线:028-86922220
TOP