name=[‘’beijing,shenzhen,nanjing]

list_names = ['shenzhen','nanjing','beijing']
#list_num = ['11','22','33']

print(list_names[0])
print(list_names[2])
print(list_names[-1])
print(list_names[-2])
print(list_names[:2]) #这里是指打印第一位到第三位（第三位不算此列）

print(list_names[-2:-1]) #从左向右数倒1不算，所以打印倒数第二个

shenzhen

beijing

beijing

nanjing

['shenzhen', 'nanjing']

['nanjing']

1.列表的下标是从0开始计算（0相当于第一位）

2.-1表示倒数第一位

3.列表中的下标是从左向右计数（读取）

list_names = ['ShenZhen','NanJing','BeiJing','YueYang','ChangSha','GuangZhou','ShaXian']
#list_num = ['11','22','33']
print(list_names[1:4]) #去下标1-4，不包括4
print(list_names[1:-1])#截取下标1~-1不包括-1
print(list_names[1:])#想获取到最后一个只能这么写
print(list_names[0:4])
print(list_names[:4]) #与[0:4]效果等同，所以可以省略0
print(list_names[0::2])#后面的2是代表每隔一个元素就获取一个。2是可以变的。如果写成[0::1]效果等同于[:]d打印全部元素
print(list_names[::2]) #效果等同于[0::2]

['NanJing', 'BeiJing', 'YueYang']

['NanJing', 'BeiJing', 'YueYang', 'ChangSha', 'GuangZhou']

['NanJing', 'BeiJing', 'YueYang', 'ChangSha', 'GuangZhou', 'ShaXian']

['ShenZhen', 'NanJing', 'BeiJing', 'YueYang']

['ShenZhen', 'NanJing', 'BeiJing', 'YueYang']

['ShenZhen', 'BeiJing', 'ChangSha', 'ShaXian']

['ShenZhen', 'BeiJing', 'ChangSha', 'ShaXian']

list_names = ['ShenZhen','NanJing','BeiJing','YueYang','ChangSha','GuangZhou','ShaXian']

list_names.append('Welcome')#默认追加到最后面
print(list_names)

['ShenZhen', 'NanJing', 'BeiJing', 'YueYang', 'ChangSha', 'GuangZhou', 'ShaXian', 'Welcome']

list_names = ['1','NanJing','a','BB','中文','@','>','a']
list_names.insert(2,'ShenZhen')
print(list_names)

['1', 'NanJing', 'ShenZhen', 'a', 'BB', '中文', '@', '>', 'a']

list_names = ['1','NanJing','a','BB','中文','@','>','a']

list_names[1]='ShenZhen'
print(list_names)

['1', 'ShenZhen', 'a', 'BB', '中文', '@', '>', 'a']

list_names = ['ShenZhen','NanJing','BeiJing','YueYang','ShenZhen','ChangSha','ShenZhen','GuangZhou','ShaXian']
print(list_names.count('ShenZhen')) #.count统计某个元素个数
list_copy=list_names.copy()
print(list_copy) #把list_names列表内容拷贝给了list_copy.
list_names.clear()
print(list_names) #清空当前列表内的元素

3

['ShenZhen', 'NanJing', 'BeiJing', 'YueYang', 'ShenZhen', 'ChangSha', 'ShenZhen', 'GuangZhou', 'ShaXian']

[]

list_del=['11','22','33','44','55','66','77','88','99']
del list_del[2:5]#删除三到五
print(list_del)

['11', '22', '66', '77', '88', '99']

1、copy模块

import copy  #运用模块
list_copy=['11','22','33','44',['Shenzhen',['11','22'],'Guangzhou','Changsha'],'55','66']
print(list_copy)
list_deep=copy.deepcopy(list_copy) #深copy（看下面注解）
list2=copy.copy(list_copy) #浅copy（看下面注解）
list_copy[4][1][0]='aa'
list_copy[2]='cc'
print(list_copy)
print(list_deep)
print(list2)

['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #原始数据

['11', '22', 'cc', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']

['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #deepcopy结果

['11', '22', '33', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #copy结果

2、copy()
list_copy=['11','22','33','44',['Shenzhen',['11','22'],'Guangzhou','Changsha'],'55','66']
list1=list_copy.copy()
print(list1)
list_copy[4][1][0]='aa'
list_copy[2]='cc'
print(list_copy)
print(list1)

['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66']

['11', '22', 'cc', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']

['11', '22', '33', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']

copy与浅copy

list_names = ['ShenZhen','NanJing',['11','22','33'],'YueYang','ChangSha','GuangZhou','ShaXian']
list_extend=['aa','bb']
list_names.extend(list_extend)
print(list_names)

['ShenZhen', 'NanJing', ['11', '22', '33'], 'YueYang', 'ChangSha', 'GuangZhou', 'ShaXian', 'aa', 'bb']

list_names = ['1','NanJing','a','BB','中文','@','>']
list_names.reverse()
print(list_names)
list_names.sort()#排序，数字>特殊字符>大写字母>小写字母>中文
print(list_names)

['>', '@', '中文', 'BB', 'a', 'NanJing', '1']

['1', '>', '@', 'BB', 'NanJing', 'a', '中文']

list_names = ['1','NanJing','a','BB','中文','@','>','a']
print(list_names.index('a'))#只返回找到的第一个下标

2

1.pop()函数（列表使用方法）

list.pop(obj=list[-1])

obj：可选参数，要移除列表元素的对象

list_del=['11','22','33','44','55','66','77','88','99']
print(list_del.pop(3))
print(list_del.pop()) #用于移除列表中的一个元素，并返还该元素的值。这里小括号不填默认是最后一个

44

99

2.remove()函数（列表使用方法）

list.remove(obj)

obj：列表中要移除的对象

list_del=['11','22','33','44','55','66','77','88','99']
list_del.remove('11') #删除指定元素
print(list_del)
list_del.remove('99')
print(list_del)

['22', '33', '44', '55', '66', '77', '88', '99']

['22', '33', '44', '55', '66', '77', '88']