写在最前:本文为个人学习成果展示,不是教程,别来参考,进来后退出就好,文章可能有误,总之,别参考这篇文章
????????????????????????????????????????????????????????????????目录 1.列表 ????????创建 ????????查找 ????????切片 ????????增加 ????????删除 ????????修改 ????????排序 2.字典????????????????键值对集合 ????????创建 ????????获取 3.元组 ????????创建
????????????????????????????????????????????????????????????????内容
# ***************************************列表*****************************************
# 列表对象的两种创建方式
lst1 = ['hello', 'world']
lst2 = [1, 2, 3, 4, 5]
lst3 = ['a', 1, 1.5, 'hi']
lst4 = list(['yes', 'no'])
print(lst1)
print(lst2[0])
print(lst3[-1])
print(lst4[0][2])
# index(查找元素,start,stop)
print('\t', lst1.index('hello'))
# 切片 列表名[start:stop;step]
print(lst2[0: 5: 2])
print(lst2[5::-1])
print('************************列表元素的增加*********************************')
lst01 = [i for i in range(1, 11)] # 列表生成式
print(lst01)
lst01.append(20)
print(lst01)
lst01.insert(0, 0)
print(lst01)
lst01.extend([30, 40, 50])
print(lst01)
print('*******************************删除**********************************')
lst01.remove(40)
print(lst01)
lst01.pop(13)
print(lst01)
lst01[0:3] = []
print(lst01)
lst02 = [1, 2, 3]
lst02.clear()
print(lst02)
del lst02
print('****************************修改*************************************')
# 法一: 直接赋值
# 法二: 切片
print('*******************************排序**********************************')
lst02 = [3, 5, 6, 2, 4, 1, 9, 7, 8, 0]
print(lst02)
lst02.sort()
print(lst02)
lst02.sort(reverse=True)
print(lst02)
lst03 = sorted(lst02)
print(lst03)
print(lst02)
# ********************************字典*********************************************
# 创建
dict1 = {'a': 1, 'b': 2, 'c': 3}
print(dict1)
student = dict(name='zhangsan', age=15)
# 获取
print(dict1['a'])
print(dict1.get('b'))
print(dict1.get('d', 0)) # 后面的为默认值
print('a' in dict1)
del student
dict1['d'] = 5
dict1['d'] = 4
print(dict1.keys())
print(dict1.values())
for item in dict1:
print(item)
# *************************************元组***************************
t1 = ('Hello', 'world')
print(t1, type(t1))
t2 = 'python', 'good' # 如果元组有多个元素,可省略()
print(t2, type(t2))
t3 = tuple(['yes', 'no'])
|