# Python list 常用操作
# 1.list 定义
li = ["a", "b", "mpilgrim", "z", "example"]
print(li) # ['a', 'b', 'mpilgrim', 'z', 'example']
print(li[1]) # b
# 2.list 负数索引
print(li[-1]) # example
print(li[-3]) # mpilgrim
print(li[1:3]) # ['b', 'mpilgrim']
print(li[1:-1]) # ['b', 'mpilgrim', 'z']
print(li[0:3]) # ['a', 'b', 'mpilgrim']
# 3.list 增加元素
li.append('new') # 加在末尾
print(li) # ['a', 'b', 'mpilgrim', 'z', 'example', 'new']
li.insert(2, 'neww') # 加在指定位置
print(li) # ['a', 'b', 'neww', 'mpilgrim', 'z', 'example', 'new']
li.extend(("two", "elements")) # 使用可迭代对象中的所有元素来扩展列表
li.extend(["two1", "elements1"])
print(li) # ['a', 'b', 'neww', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements', 'two1', 'elements1']
# 4.list 搜索
print(li.index("new")) # 6
# print(li.index("new123")) # 无该元素会报错
# 5.list 删除元素
li.remove('elements1') # 删除首次出现的一个值
print(li) # ['a', 'b', 'neww', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements', 'two1']
# li.remove('elements1') # ist 中没有找到值, Python 会引发一个异常
print(li.pop()) # two1; pop 会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值
print(li) # ['a', 'b', 'neww', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
# 6.list 运算符
li = li + ['heng', 'taozi']
print(li) # ['a', 'b', 'neww', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements', 'heng', 'taozi']
li = [1, 2] * 3
print(li) # 1, 2, 1, 2, 1, 2]
# 7.使用join链接list成为字符串
params = {"server": "mpilgrim", "database": "master", "uid": "sa", "pwd": "secret"}
ll = ['{}={}'.format(k, v) for k, v in params.items()]
print(ll) # ['server=mpilgrim', 'database=master', 'uid=sa', 'pwd=secret']
str = ";".join(ll)
print(str) # server=mpilgrim;database=master;uid=sa;pwd=secret
# 8.list 分割字符串
s = str.split(';')
print(s) # ['server=mpilgrim', 'database=master', 'uid=sa', 'pwd=secret']
# 9.list 的映射解析
li_1 = [1, 9, 8, 4]
print(li_1)
li_2 = [elem * 2 for elem in li_1]
print(li_2)
# 10.dictionary 中的解析
params = {"server": "mpilgrim", "database": "master", "uid": "sa", "pwd": "secret"}
print(params.keys()) # dict_keys(['server', 'database', 'uid', 'pwd'])
print(params.values()) # dict_values(['mpilgrim', 'master', 'sa', 'secret'])
print(params.items()) # dict_items([('server', 'mpilgrim'), ('database', 'master'), ('uid', 'sa'), ('pwd', 'secret')])
print([k for k, v in params.items()]) # ['server', 'database', 'uid', 'pwd']
print([v for k, v in params.items()]) # ['mpilgrim', 'master', 'sa', 'secret']
print(["%s=%s" % (k, v) for k, v in params.items()]) # ['server=mpilgrim', 'database=master', 'uid=sa', 'pwd=secret']
# 11.list 过滤
li_3 = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]
print([elem for elem in li_3 if len(elem) > 1]) # ['mpilgrim', 'foo']
print([elem for elem in li_3 if elem != 'b']) # ['a', 'mpilgrim', 'foo', 'c', 'd', 'd']
print([elem for elem in li_3 if li_3.count(elem) == 1]) # ['a', 'mpilgrim', 'foo', 'c']
# 12.其它常用内置函数
l = [3, 2, 3, 7, 8, 1]
print(l.count(3)) # 统计
l.reverse() # 原地反转
print(l) # [1, 8, 7, 3, 2, 3]
l.sort() # 原地正序
print(l) # [1, 2, 3, 3, 7, 8]
l.sort(reverse=True) #原地倒序
print(l) # [8, 7, 3, 3, 2, 1]
Python学习打卡--day34(基础练习: Python list 常用操作)
最新推荐文章于 2022-04-01 11:07:11 发布