列表
- 可通过[]来创建一个list结构
- 里面可以放任何类型数据,没有长度限制
tang = []
tang2 = [1,2,3,4]
tang = list()
tang = list([1,2,3])
tang = [1,2,3]
len(tang)
a = [123,456]
b = ['bai','long']
print (a + b)
print (a*3)
a[0:]
a[:] = ['bai','long','ma']
a[0:2]
del a[3:]
a = [1,2,3,4]
3 in a
a = [1,2,[3,4]]
print (a[2])
print (a[2][1])
tang =['apple','banana','apple','apple','apple','banana','banana']
tang.count('apple')
tang.index('apple')
No.1:
tang = []
tang.append('bai','long','ma')
tang.insert(2,'python')
tang.remove('bai')
tang.pop(1)
No.2:
tang = [1,2,3,9,6,3,2]
tang.sort()
print (tang)
tang = [1,2,3,9,6,3,2]
tang2 = sorted(tang)
print (tang,tang2)
tang = [1,2,3]
tang.reverse()
print (tang)
字典结构
- 可通过{}来创建一个字典结构
- 内容为key:value
#创建方式1:
tang = {}
#创建方式2:
tang = dict()
#字典结构操作:
No.1:
tang = {}
tang['first'] = 123
tang['python'] = 456
print (tang) #{'first': 123, 'python': 456}
tang['python'] = 789 #改变字典里的内容
#可以创建字典的同时赋值
tang = {'bai':123,'long':456,'ma':789}
No.2:
tang_value = [1,2,3]
tang = {}
#字典里可以存放列表
tang2['bai'] = tang_value
tang2['long'] = 3
tang2['ma'] = '4'
print (tang2) #{'bai': [1, 2, 3], 'long': 3, 'ma': '4'}
#创建字典
tang3 = dict([('bai',123),('long',456)])
tang3['bai'] += 1
print (tang3) #{'bai': 124, 'long': 456}
tang3.get('bai') #取'bai'键对应的值124
tang3.get('test','mei') #取'test'键对应的值,没有则返回'mei'
tang3.pop('bai') #弹出'bai':124,返回124
print (tang3) #{'long': 456}
del tang3('long') #删除键值对
print (tang3)
No.3:
tang = {'tang':123,'bai':456}
tang2 = {'tang':789,'python':888}
tang.update(tang2)
print (tang) #{'tang': 789, 'bai': 456, 'python': 888}
#查看'tang'是否在字典中,在则返回True,否则返回False
'tang' in tang
#得到所有的键,返回dict_keys(['tang', 'bai', 'python'])
tang.keys()
#得到所有的值,返回dict_values([789, 456, 888])
tang.values()
tang.items() #得到字典里的内容