Python list common operations summary (dry goods!)

1. List definition

list = ['a', 'b', '123', 'z', '456']
print(list)
#输出结果:['a', 'b', '123', 'z', '456']
print(list[1])
#输出结果:b

2. List negative index

list = ['a', 'b', '123', 'z', '456']
print(list[-1])
#输出结果:456
print(list[-3])
#输出结果:123

3. List slice

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 adds elements

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 delete elements

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 search

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 operator

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. Use join to link the list into a string

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 split string

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 mapping

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. Parsing in dict (dictionary) (returned in list form)

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']

12, list filtering

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']
Published 31 original articles · won 123 · views 7297

Guess you like

Origin blog.csdn.net/weixin_43347550/article/details/105536569