list = ['a', 'b', '123', 'z', '456']
print(list)
#输出结果:['a', 'b', '123', 'z', '456']
print(list[1])
#输出结果:b
2、list负数索引
list = ['a', 'b', '123', 'z', '456']
print(list[-1])
#输出结果:456
print(list[-3])
#输出结果:123
3、list切片
list = ['a', 'b', '123', 'z', '456']
print(list[0:3])
#输出结果:['a', 'b', '123']
print(list[1:-1])
#输出结果:['b', '123', 'z']
print(list[::-1]) # 重点:逆序输出list中元素
#输出结果:['456', 'z', '123', 'b', 'a']
4、list增加元素
list = ['a', 'b', '123', 'z', '456']
list.append('new1') #在list末尾添加
print(list)
#输出结果:['a', 'b', '123', 'z', '456', 'new1']
list.insert(2, 'new2') #在指定位置上添加元素
print(list)
#输出结果:['a', 'b', 'new2', '123', 'z', '456', 'new1']
list.extend(['new3', 'new4']) #添加一个列表
print(list)
#输出结果:['a', 'b', 'new2', '123', 'z', '456', 'new1', 'new3', 'new4']
5、list删除元素
list = ['a', 'b', 'new', '123', 'z', '456', 'new', 'new3', 'new4']
list.remove('z')
print(list)
#输出结果:['a', 'b', 'new', '123', '456', 'new', 'new3', 'new4']
list.remove('new') #删除首次出现的new
print(list)
#输出结果:['a', 'b', '123', '456', 'new', 'new3', 'new4']
# list.remove('c') #list 中没有找到值,会引发异常list.pop
# print(list)
#输出结果:ValueError: list.remove(x): x not in list
pop = list.pop() #pop 会做两件事:删除 list 的最后一个元素, 然后返回删除的元素的值
print(pop)
#输出结果:new4
print(list)
#输出结果:['a', 'b', '123', '456', 'new']
6、list搜索
list = ['a', 'b', 'new', '123', 'z', '456', 'new', 'new3', 'new4']
index1 = list.index('456')
print(index1)
#输出结果:5
index2 = list.index('new') #返回第一个匹配的元素位置
print(index2)
#输出结果:2
index3 = list.index('c') #list 中没有找到该值,则会引发异常
print(index3)
#输出结果:ValueError: 'c' is not in list
7、list运算符
list = ['a', 'b', '123', 'z', '456']
list = list + ['new', 'new3']
print(list)
#输出结果:['a', 'b', '123', 'z', '456', 'new', 'new3']
list += ['two']
print(list)
#输出结果:['a', 'b', '123', 'z', '456', 'new', 'new3', 'two']
list = [1, 2] * 3
print(list)
#输出结果:[1, 2, 1, 2, 1, 2]
8、使用join链接list成为字符串
params = {'server':'abc', 'database':'master', 'uid':'123', 'pwd':'456'}
x = ['%s=%s' % (k, v) for k, v in params.items()]
print(x)
#输出结果:['server=abc', 'database=master', 'uid=123', 'pwd=456']
y = ';'.join(['%s=%s' % (k, v) for k, v in params.items()])
print(y)
#输出结果:server=abc;database=master;uid=123;pwd=456
#Note:join 只能用于元素是字符串的 list;它不进行任何的类型强制转换。
9、list分割字符串
list = ['server=abc', 'database=master', 'uid=123', 'pwd=456']
x = ';'.join(list)
print(x)
#输出结果:server=abc;database=master;uid=123;pwd=456
y = x.split(';') #split 与 join 正好相反,它将一个字符串分割成多元素list
print(y)
#输出结果:['server=abc', 'database=master', 'uid=123', 'pwd=456']
z = x.split(';', 1) #第二个参数为要分割的次数
print(z)
#输出结果:['server=abc', 'database=master;uid=123;pwd=456']
10、list的映射
list = [1, 5, 3, 10]
print([elem*2 for elem in list])
#输出结果:[2, 10, 6, 20]
print(list)
#输出结果:[1, 5, 3, 10]
list = [elem*2 for elem in list]
print(list)
#输出结果:[2, 10, 6, 20]
11、dict(字典)中的解析(返回的为list形式)
params = {'server':'abc', 'database':'master', 'uid':'123', 'pwd':'456'}
print(params.keys())
#输出结果:dict_keys(['server', 'database', 'uid', 'pwd'])
print(params.values())
#输出结果:dict_values(['abc', 'master', '123', '456'])
print(params.items())
#输出结果:dict_items([('server', 'abc'), ('database', 'master'), ('uid', '123'), ('pwd', '456')])
print([k for k, v in params.items()])
#输出结果:['server', 'database', 'uid', 'pwd']
print([v for k, v in params.items()])
#输出结果:['abc', 'master', '123', '456']
print(['%s=%s' % (k, v) for k, v in params.items()])
#输出结果:['server=abc', 'database=master', 'uid=123', 'pwd=456']
print([{k:v} for k, v in params.items()])
#输出结果:[{'server': 'abc'}, {'database': 'master'}, {'uid': '123'}, {'pwd': '456'}]
12、list过滤
list = ['a', 'b', '123', 'z', '456', 'b', 'd', 'd']
print([elem for elem in list if len(elem) > 1]) #过滤掉 list 里字符串长度小于等于1的元素
#输出结果:['123', '456']
print([elem for elem in list if elem != 'b']) #过滤掉'b'元素
#输出结果:['a', '123', 'z', '456', 'd', 'd']
print([elem for elem in list if list.count(elem) == 1]) #过滤掉除了 list 里只出现过一次的元素
#输出结果:['a', '123', 'z', '456']