>>> list1 = ['python', 2018, 'python3', 1994] >>> list1 ['python', 2018, 'python3', 1994] >>> list2 = [1, 2, 3, 4] >>> list2 [1, 2, 3, 4] >>> list3 = ['a', 'b', 'c', 'd'] >>> list3 ['a', 'b', 'c', 'd']
>>> list1 = ['python', 2018, 'python3', 1994] >>> len(list1) 4
>>> list1 = ['python', 2018, 'python3', 1994] >>> list1[0] 'python'
注意:当索引超出范围时,Python会报一个IndexError错误,所以,要确保索引不要越界,记得最后一个元素的索引是len(list1) - 1。
2. 获取最后一个元素
>>> list1[-1] 1994
>>> list1 = ['python', 2018, 'python3', 1994] >>> list1[0:3] ['python', 2018, 'python3'] >>> list1[:3] #如果第一个索引是0,可以省略 ['python', 2018, 'python3']
倒数切片:
>>> list1[-2:] #获取后2个元素 ['python3', 1994] >>> list1[-2:-1] # 获取倒数第二个元素 ['python3']
前4个元素,每两个取一个:
>>> list1[:4:2] ['python', 'python3']
所有元素,每3个取一个:
>>> list1[::3] ['python', 1994]
原样复制一个列表:
>>> list1[:] ['python', 2018, 'python3', 1994]
>>> list1 = ['python', 2018, 'python3', 1994] >>> list2 = [1, 2, 3, 4] >>> list4 = list1 + list2 >>> list4 ['python', 2018, 'python3', 1994,1, 2, 3, 4]
用索引来修改元素
>>> list1 ['python', 2018, 'python3', 1994] >>> list1[1] = 2017 >>> list1 ['python', 2017, 'python3', 1994]
list=['python', 2018, 'python3', 1994] list.remove('python') print(list) [2018, 'python3', 1994]
list=['python', 2018, 'python3', 1994] del list[0] print(list) [2018, 'python3', 1994]
list=['python', 2018, 'python3', 1994] latlist=list.pop() print(latlist) print(list) 1994 ['python', 2018, 'python3']
2)通过索引删除
list=['python', 2018, 'python3', 1994] latlist=list.pop(1) print(latlist) print(list) 2018 ['python', 'python3', 1994]
if 'a' in newList: print ('find a in newList') else : print ('do not find a in newList')
print (list.index('python')) 0
newList=[1,3,4,2] newList.sort() print(newList) [1,2,3,4]
newList.sort() newList.reverse() print(newList) [4,3,2,1]
newList.sort(reverse = True) print(newList) [4,3,2,1]
old = [5,4,3,2,1] new = sorted(old) print(old) print(new) [5,4,3,2,1] [1,2,3,4,5]
>>> list1 ['python', 2017, 'python3', 1994] >>> list1.clear() >>> list1 []
list1 = ['Google', 'Runoob', 'Taobao'] list1.append('Baidu') print ("更新后的列表 : ", list1) 更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
list1 = ['Google', 'Runoob', 'Taobao'] list2=list(range(5)) # 创建 0-4 的列表 list1.extend(list2) # 扩展列表 print ("扩展后的列表:", list1) 扩展后的列表: ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]
list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('列表插入元素后为 : ', list1) 列表插入元素后为 : ['Google', 'Baidu', 'Runoob', 'Taobao']
list.count(obj): 返回元素在列表中出现的次数,一个都没有返回0
aList = [123, 'Google', 'Runoob', 'Taobao', 123]; print ("123 元素个数 : ", aList.count(123)) print ("Runoob 元素个数 : ", aList.count('Runoob')) 123 元素个数 : 2 Runoob 元素个数 : 1
copy.copy(li)
浅拷贝,对地址的拷贝,被拷贝的列表中可变类型改变,新的列表也会改变。不可变类型改变,新的列表不变copy.deepcopy(li)
深拷贝,对值的拷贝,产生新的列表与被拷贝的列表互不影响li = old_li
赋值,两个变量都指向同一个内存块,修改li会对old_li产生影响,同理,修改old_li也会对li产生影响lists = [1, 2, 3, 4, 5] print("--------# 只遍历值------------") # 只遍历值 for i in lists: print(i) print("--------# 逆序遍历--1----------") # 逆序遍历 for i in lists[::-1]: print(i) print("--------# 逆序遍历--2----------") # 逆序遍历 for i in range(len(lists), 0, -1): print(i) print("--------# 遍历键和值--2----------") # 遍历键和值 for idx, val in enumerate(lists): print(idx,':',val) print("--------# 遍历键----------") # 只遍历键 for idx in range(0, len(lists)): print(idx)