user = [] user = list()
# 将字符串转成列表
>>> list('abcde')
['a', 'b', 'c', 'd', 'e']
# 将元祖转成列表
>>> list(('a','b','c'))
['a', 'b', 'c']
>>> users = ['a','b','c','d','e'] # 可以使用整数来获取某个元素 >>> users[0] 'a' # 可以使用负整数来表示从尾部获取某个元素 >>> users[-1] 'e' # 数组越界会报错 >>> users[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> users[-100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range # 修改某个元素 >>> users[0] = 'wdd' >>> users ['wdd', 'b', 'c', 'd', 'e'] >>>
>>> users ['wdd', 'b', 'c', 'd', 'e'] # 正常截取 注意这里并不会截取到users[2] >>> users[0:2] ['wdd', 'b'] # 也可从尾部截取 >>> users[0:-2] ['wdd', 'b', 'c'] # 这样可以获取所有的元素 >>> users[:] ['wdd', 'b', 'c', 'd', 'e'] # 也可以加上步长参数 >>> users[0:4:2] ['wdd', 'c'] # 也可以通过这种方式去将列表取反 >>> users[::-1] ['e', 'd', 'c', 'b', 'wdd'] # 注意切片时,偏移量可以越界,越界之后不会报错,仍然按照界限来处理 例如开始偏移量如果小于0,那么仍然会按照0去计算。 >>> users ['wdd', 'b', 'c', 'd', 'e'] >>> users[-100:3] ['wdd', 'b', 'c'] >>> users[-100:100] ['wdd', 'b', 'c', 'd', 'e'] >>>
>>> users
['wdd', 'b', 'c', 'd', 'e']
>>> users.append('ddw')
>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw']
>>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw'] >>> names ['heihei', 'haha'] >>> users.extend(names) >>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha'] >>> users += names >>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']
>>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha'] >>> users.insert(0,'xiaoxiao') >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha'] >>> users.insert(-1,'-xiaoxiao') >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha'] # 下面-100肯定越界了 >>> users.insert(-100,'-xiaoxiao') >>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha'] # 下面100也是越界了 >>> users.insert(100,'-xiaoxiao') >>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
>>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] >>> del users[0] >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] >>> del users[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>> del users[-100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range
>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 删除指定值'c'
>>> users.remove('c')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 删除不存在的值会报错
>>> users.remove('alsdkfjalsdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
# 如果该值存在多个,那么只能删除到第一个
>>> users.remove('haha')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
>>> users ['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] # 删除最后的元素 >>> users.pop() '-xiaoxiao' >>> users ['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] # 如果列表本身就是空的,那么pop时会报错 >>> user.pop(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty list >>> users.pop(0) 'xiaoxiao' >>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] # 越界时也会报错 >>> users.pop(100) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range
# index只会返回第一遇到该值得位置
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.index('heihei')
5
# 如果该值不存在,也会报错
>>> users.index('laksdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'laksdf' is not in list
>>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] >>> 'wdd' in users True
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.count('heihei')
2
>>> users.count('h')
0
>>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] >>> ','.join(users) 'wdd,b,d,e,ddw,heihei,heihei,-xiaoxiao,haha' >>> user [] >>> ','.join(user) ''
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
# 默认是升序排序
>>> users.sort()
>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
# 加入reverse=True, 可以降序排序
>>> users.sort(reverse=True)
>>> users
['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']
# 通过匿名函数,传入函数进行自定义排序
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}]
>>> students.sort(key=lambda item: item['age'])
>>> students
[{'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}, {'name': 'wdd', 'age': 343}]
>>> students.sort(key=lambda item: item['age'], reverse=True)
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'jik', 'age': 90}, {'name': 'ddw', 'age': 43}]
>>>
>>> users ['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao'] >>> users.reverse() >>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
>>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> users2 = users.copy() >>> users2 ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>>
>>> users2 ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> users2.clear() >>> users2 []
>>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> len(users) 9
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有