>>> all_users = ["qiwsir","github"]
>>> all_users.append("io")
>>> all_users
['qiwsir', 'github', 'io']
>>> all_users
['qiwsir', 'github', 'io']
>>> all_users.insert("python") #list.insert(i,x),要求有两个参数,少了就报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: insert() takes exactly 2 arguments (1 given)
>>> all_users.insert(0,"python")
>>> all_users
['python', 'qiwsir', 'github', 'io']
>>> all_users.insert(1,"http://")
>>> all_users
['python', 'http://', 'qiwsir', 'github', 'io']
>>> length = len(all_users)
>>> length
5
>>> all_users.insert(length,"algorithm")
>>> all_users
['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm']
list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
>>> all_users
['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm']
>>> all_users.remove("http://")
>>> all_users #的确是把"http://"删除了
['python', 'qiwsir', 'github', 'io', 'algorithm']
>>> all_users.remove("tianchao") #原list中没有“tianchao”,要删除,就报错。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> all_users
['python', 'qiwsir', 'github', 'io', 'algorithm']
>>> "python" in all_users #这里用in来判断一个元素是否在list中,在则返回True,否则返回False
True
>>> if "python" in all_users:
... all_users.remove("python")
... print all_users
... else:
... print "'python' is not in all_users"
...
['qiwsir', 'github', 'io', 'algorithm'] #删除了"python"元素
>>> if "python" in all_users:
... all_users.remove("python")
... print all_users
... else:
... print "'python' is not in all_users"
...
'python' is not in all_users #因为已经删除了,所以就没有了。
>>> all_users ['qiwsir', 'github', 'io', 'algorithm'] >>> all_users.pop() #list.pop([i]),圆括号里面是[i],表示这个序号是可选的 'algorithm' #如果不写,就如同这个操作,默认删除最后一个,并且将该结果返回 >>> all_users ['qiwsir', 'github', 'io'] >>> all_users.pop(1) #指定删除编号为1的元素"github" 'github' >>> all_users ['qiwsir', 'io'] >>> all_users.pop() 'io' >>> all_users #只有一个元素了,该元素编号是0 ['qiwsir'] >>> all_users.pop(1) #但是非要删除编号为1的元素,结果报错。注意看报错信息 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range #删除索引超出范围,就是1不在list的编号范围之内
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有