其他分享
首页 > 其他分享> > 列表

列表

作者:互联网

任意对象的有序集合

通过偏移读取

可变长度,异构以及嵌套

#可变序列
#对象引用数组

l = [1,2,3]
print(len(l)) # 3
print([1,2,4] + [5,6,7]) # [1, 2, 4, 5, 6, 7]
print(['hi'] 4) # ['hi', 'hi', 'hi', 'hi']
print([1,2] + list('34')) # [1, 2, '3', '4']
print(3 in [1,2,3]) # True
#列表解析
for i in [1,2,3]:
print(i,end='') # 123
print([x
4 for x in [1,2,3]]) # [4, 8, 12]
l = []
for i in [1,2,3]:
l.append(i *4)
print(l) # [4, 8, 12]
print(list(map(abs,[1,-2,-3,-4]))) # [1, 2, 3, 4]
#索引,分片,矩阵
l = ['spam','Spam','SPAM']
print(l[0]) # spam
print(l[-2]) # Spam
print(l[1:]) # ['Spam', 'SPAM']
l = [[1,2,3],[4,5,6],[7,8,9]]
print(l[1]) # [4, 5, 6]
l = [1,2,3]
l[0] = 'spam'
print(l) # ['spam', 2, 3]
l[:2] = ['eat','more']
print(l) # ['eat', 'more', 3]
l[2] = [4,5]
print(l) # ['eat', 'more', [4, 5]]

#列表方法调用
l.append('lux')
print(l) # ['eat', 'more', [4, 5], 'lux']
l =[3,6,3,2,3,4,5,6]
l.sort()
print(l) # [2, 3, 3, 3, 4, 5, 6, 6]
l = ['abc','SDF','Dcf']
l.sort(key = str.lower)
print(l) # ['abc', 'Dcf', 'SDF']
l.sort(key = str.lower,reverse = True)
print(l) # ['SDF', 'Dcf', 'abc']
l = ['abc','SDF','Dcf']
print(sorted(l,key = str.lower,reverse = True)) #['SDF', 'Dcf', 'abc']
l = ['abc','SDF','Dcf']
print(sorted([x.lower() for x in l], reverse = True)) # ['sdf', 'dcf', 'abc']
l = [1,2]
l.extend([3,4,5])
print(l) # [1, 2, 3, 4, 5]
print(l.pop()) # 5
l.reverse()
print(l) # [4, 3, 2, 1]
print(list(reversed(l))) # [1, 2, 3, 4]
l = []
l.append(1)
l.append(2)
print(l) # [1, 2]
print(l.pop()) # 2
print(l.pop()) # 1
print(l) # []
l = ['abc','SDF','Dcf']
print(l.index('abc')) # 0
l.insert(1,'ddd')
print(l) # ['abc', 'ddd', 'SDF', 'Dcf']
l.remove('ddd')
print(l) # ['abc', 'SDF', 'Dcf']
l.pop(1)
print(l) # ['abc', 'Dcf']
l = ['abc','SDF','Dcf']
del l[0]
print(l) # ['SDF', 'Dcf']
del l[1:]
print(l) # ['SDF']
l = ['abc','SDF','Dcf']
l[1:] = []
print(l) # ['abc']
l[0] = []
print(l) # [[]]

标签:abc,Dcf,pop,列表,SDF,hi,print
来源: https://blog.51cto.com/14418623/2417751