1、list[0]与list[0:1]
前者返回一个值,后者是列表
a = [2, 0, 6]
a[0] # 2
a[0:1] # [2]
2、b = a[:]与b = a
前者产生一个新列表,与原列表之后是否修改无关,a、b是两个列表
后者只是为原列表加了个标签,a、b都是一个列表
a = [0,6,2,5,7,4,9,3]
b = a[:]
c = a
print('b',b) # b [0, 6, 2, 5, 7, 4, 9, 3]
print('c',c) # c [0, 6, 2, 5, 7, 4, 9, 3]
a.sort()
print('a',a) # a [0, 2, 3, 4, 5, 6, 7, 9]
print('b',b) # b [0, 6, 2, 5, 7, 4, 9, 3]
print('c',c) # c [0, 2, 3, 4, 5, 6, 7, 9]
a = [1,4,2,5]
b = a
a = [2]
print(b) # b [1,4,2,5]
3、获取列表元素
a = ['1.a一','2.b二','3.c三']
a[0] # 1.a一
a[0][1] # .
网友评论