[python] Adding or Deleting elements in a list or set
2015-12-26 05:19
543 查看
Adding elements in a list
Deleting elements in a list
Adding elements in a set
Deleting elements in a set
Note that using + means two things: adding elements to the existing list and make a copy of it.
(2)using append by a.append(…)
This means just adding elements to the existing list(end).
(3)using extend by a.extend(…)
It can only be used when adding a list to the existing list.
(4)using insert by a.insert(a, …)
Insert can adding some elements to the ath place of the existing list.
delete the element of a fixed index.
(2)using pop by a.pop(…)
If without parameter, it means deleting the last element. Otherwise, it means delete the given index, like del.
(3) using remove(…) by a.remove(…)
remove the fixed content of a list
Adding elements into a
(2) using update by b.update(…)
Can be used only when adding a set or a list
Delete a fixed element in or not in the set.
(2) using remove by b.remove(…)
Can be just to delete the existing element in the set.
(3) using pop by b.pop(…)
Since set is with no order, it delete one element randomly.
(4) using clear by b.clear()
Delete all elements in the set
Deleting elements in a list
Adding elements in a set
Deleting elements in a set
1 Adding elements in a list
(1) using + by a=a+[...]Note that using + means two things: adding elements to the existing list and make a copy of it.
(2)using append by a.append(…)
This means just adding elements to the existing list(end).
(3)using extend by a.extend(…)
It can only be used when adding a list to the existing list.
(4)using insert by a.insert(a, …)
Insert can adding some elements to the ath place of the existing list.
a = [1,2,3] a = a + [4] print(a)#[1, 2, 3, 4] a.append(5) print(a)#[1, 2, 3, 4, 5] a.extend([6,7]) print(a)#[1, 2, 3, 4, 5, 6, 7] a.insert(0,8) print(a)#[8, 1, 2, 3, 4, 5, 6, 7] a.append([9,10]) print(a)#[8, 1, 2, 3, 4, 5, 6, 7, [9, 10]]
2. Deleting elements in a list
(1) using del by del a[…]delete the element of a fixed index.
(2)using pop by a.pop(…)
If without parameter, it means deleting the last element. Otherwise, it means delete the given index, like del.
(3) using remove(…) by a.remove(…)
remove the fixed content of a list
del a[0] print(a)#[1, 2, 3, 4, 5, 6, 7, [9, 10]] a.pop() print(a)#[1, 2, 3, 4, 5, 6, 7] a.pop(0) print(a)#[2, 3, 4, 5, 6, 7] a.remove(3) print(a)#[2, 4, 5, 6, 7]
3. Adding elements in a set
(1) using add by b.add(…)Adding elements into a
(2) using update by b.update(…)
Can be used only when adding a set or a list
b = {1,2} b.add(3) print(b)#{1, 2, 3} b.add(1) print(b)#{1, 2, 3} b.update({4,5}) print(b)#{1, 2, 3, 4, 5} b.update({6},{7,8}) print(b)#{1, 2, 3, 4, 5, 6, 7, 8} b.update([9,10]) print(b){1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
4. Deleting elements in a set
(1) using discard by b.discard(…)Delete a fixed element in or not in the set.
(2) using remove by b.remove(…)
Can be just to delete the existing element in the set.
(3) using pop by b.pop(…)
Since set is with no order, it delete one element randomly.
(4) using clear by b.clear()
Delete all elements in the set
b.discard(1) print(b)#{2, 3, 4, 5, 6, 7, 8, 9, 10} b.discard(11) print(b)#{2, 3, 4, 5, 6, 7, 8, 9, 10} b.remove(2) print(b)#{3, 4, 5, 6, 7, 8, 9, 10} b.pop() print(b)#{4, 5, 6, 7, 8, 9, 10} b.clear() print(b)#set()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法