"""
1. 列表、字符串、元组、字典之间的相互转换
2.列表的遍历方式
"""
print("---------------------第一题---------------------")
str1 = "hello world"
list1 = [11, 22, 33, 44]
list2 = ['aa', 'bb', 'cc', 'dd']
tuple1 = (1, 2, 3, 4)
dict1 = {'name': 'zhangsan', 'age': 18, 'gender': 'boy'}
print('字符串转列表:',list(str1))
print('字符串转列表:',str1.split())
print("元组转列表:",list(tuple1))
print("字典转列表",list(dict1.keys()))
print("字典转列表:",list(dict1.values()))
print("字符串转元组",tuple(str1))
print("列表转元组:",tuple(list1))
print("两个数组合并成一个字典:",dict(zip(list1, list2)))
print("列表转字符串:",''.join(list2))
print("字符串转字典:dict = eval(str)")
print(str(list1))
print(type(str(list1)))
print("---------------------第二题---------------------")
print('方法一')
for i in list2:
print(i)
print('方法二')
for i in range(0,int(len(list2))):
print(list2[i])
print('方法三')
i = 0
while i < int(len(list2)):
print(list2[i])
i += 1
"""
1. 字符串截取子串
2. 字典的遍历方式
"""
print("---------------------第一题---------------------")
# 字符串截取字串
str1 = 'aaabbbcccdddeeefff'
print('字符串截取子串:',str1[3:9])
print('字符串截取子串:',str1[::3])
print('字符串截取子串:',str1[-1:-7:-1])
print("---------------------第二题---------------------")
# 字典的遍历方式
dict1 = {'name': 'zhangsan', 'age': 18, 'gender': 'boy'}
print('***打印key***')
for i in dict1.keys():
print(i)
print('***打印value***')
for i in dict1.values():
print(i)
print('***打印(key,value)***')
for i in dict1.items():
print(i)
print('***打印key:value***')
for k,v in dict1.items():
print(k,':',v)
"""
1.列表的增加、删除、更新
2.字典的增加、删除、更新
"""
print("---------------------第一题---------------------")
# 列表的增加、删除、更新
list1 = ['aa', 'bb', 'cc', 'dd']
list1.append(11)
list1.append(22)
list1.append(33)
print('append新增后的列表:',list1)
list1.pop()
print('pop删除后的列表:',list1)
list1.remove(11)
print('remove删除后的列表:',list1)
list1[2] = 'ss'
print('更新后的列表:',list1)
print("---------------------第二题---------------------")
# 字典的增加、删除、更新
dict1 = {'name': 'zhangsan', 'age': 18, 'gender': 'boy'}
dict1['Class'] = 'First'
print('新增后的字典:',dict1)
dict1['age'] = 16
print('更新后的字典:',dict1)
del dict1['name'] # 删除键name
print('del删除后的字典:',dict1)
dict1.clear() #清空字典
print('clear清空后的字典:',dict1)
"""
1. 列表和元组的区别
2. 访问列表的最后一个元素
"""
print("---------------------第一题---------------------")
# 列表和元祖的区别
list1 = [11, 22, 33, 44, 55]
print("列表:是可变的,内容和长度都可变")
list1[3] = 66
print(list1)
list1.append(77)
print(list1)
"""
打印结果:
列表:是可变的,内容和长度都可变
[11, 22, 33, 66, 55]
[11, 22, 33, 66, 55, 77]
"""
print('********************')
tuple1 = ('aa', 'bb', 'cc', 'dd', 'ee')
tuple2 = (11, 22, 33, 44)
tuple3 = ([111, 222, 333], 'a', 'b')
print("元组:是不可变的")
#tuple1[1] = 22
#print('tuple1')
"""
修改元组内容会报错
Traceback (most recent call last):
File "C:/2020-04-24.py", line 31, in <module>
tuple1[1] = 22
TypeError: 'tuple' object does not support item assignment
"""
tuple4 = tuple1 + tuple2
print(tuple4)
# 给元组内的列表新增元素
tuple3[0].append(999)
print(tuple3)
print("---------------------第二题---------------------")
# 访问列表的最后一个元素
list2 = [99, 88, 77, 66]
length = len(list2)-1
print('方法一:',list2[length])
print('方法二:',list2[-1:][0])
list2.reverse()
print('方法三:',list2[0])
网友评论