Python list,tuple,dict,set高级变量常用方法
list列表
增加
- append 在列表中追加,一次只能加一个
- insert 按索引插入,一次只能插一个
- extend 迭代追加到列表中
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.append(2)
print(list1)
list1.insert(1, 4)
print(list1)
list1.extend(list2)
print(list1)
输出:
[1, 2, 3, 2]
[1, 4, 2, 3, 2]
[1, 4, 2, 3, 2, 4, 5, 6]
删除
- remove 按照元素值删除,一次只能删除一个
- pop 按索引删除,也可以按切片跟步长删除,默认删除最后一个(输出返回值)
- clear 清空列表
- del 删除整个列表
list1 = [1, 2, 3, 4, 5, 6]
list1.remove(3)
print(list1)
list1.pop()
print(list1)
list1.pop(2) #或者del list1[2]不推荐
print(list1)
list1.clear()
print(list1)
del list1
print(list1)
输出:
[1, 2, 4, 5, 6]
[1, 2, 4, 5]
[1, 2, 5]
[]
NameError: name 'list1' is not defined
修改
- 按索引去改
- 按切片步长去改
list1 = [1,2,3,4,5,6]
list1[2] = 9
print(list1)
list1[1:4] = [11,12,13]
print(list1)
输出:
[1, 2, 9, 4, 5, 6]
[1, 11, 12, 13, 5, 6]
查询
- 按索引,切片加步长去查
- 用for循环查
list1 = [1,2,3,4,5,6]
list2 = list1[0:5:2]
print(list2)
for i in list1:
print(i)
输出:
[1, 3, 5]
1
2
3
4
5
6
其他方法
len() 查询列表长度
count 统计某个元素出现的个数
index 通过元素找索引
sort 排序列表,括号里接reverse=True就是从大到小
reverse 倒叙列表
enumerate 用for循环,给序列加序号
list1=[1,2,3,4,5,6,7,4,5,4]
print(len(list1))
print(list1.count(4))
print(list1.index(2))
print(list1.index(4)) #默认显示第一个匹配的元素的索引
list1.sort(reverse=True)
print("降序:", list1
for index,value in enumerate(list1):
print("{0},{1}".format(index,value))
输出:
10
3
1
3
降序: [7, 6, 5, 5, 4, 4, 4, 3, 2, 1]
0,7
1,6
2,5
3,5
4,4
5,4
6,4
7,3
8,2
9,1
tuple元组
tuple元组与列表类似,不同之处在于元组的元素不能修改
查询
- 按索引去查
- 按切片步长去查
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
输出:
tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)
修改
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz') #tup1[0] = 100 该方法是非法的
tup3 = tup1 + tup2; #创建一个新的元组
print (tup3)
输出:
(12, 34.56, 'abc', 'xyz')
删除
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
列表和元祖的转换(如想要保护数据,可把list转换成元祖,如修改可转成列表)
tup = ('Google', 'Runoob', 1997, 2000)
print (tup)
del tup;
print ("删除后的元组 tup : ")
print (tup)
输出:
NameError: name 'tup' is not defined
dict字典
增加
正常方式写入即可,有则不动,无则增加
dict1 = {"name": "小明", "age": 18, "height": 1.75}
dict1["wegit"] = 64.5
print(dict1)
输出:
{'name': '小明', 'age': 18, 'height': 1.75, 'wegit': 64.5}
修改
- update 把另一个键值队合并到一个,相同的覆盖,没有的添加
dict2 = {"name": "小华", "age": 18, "curriculum": "English"}
dict1.update(dict2)
print(dict1)
输出:
{'name': '小华', 'age': 18, 'height': 1.75, 'wegit': 64.5, 'curriculum': 'English'}
删除
- pop 按key删
- popitem 删除最后一个
- clear 清空
- del 删除key或全部删除
dict1 = {'name': '小华', 'age': 18, 'height': 1.75, 'wegit': 64.5, 'curriculum': 'English'}
dict1.popitem()
print(dict1)
dict1.pop("wegit")
print(dict1)
del dict1["name"]
print(dict1)
dict1.clear()
print()
输出:
{'name': '小华', 'age': 18, 'height': 1.75, 'wegit': 64.5}
{'name': '小华', 'age': 18, 'height': 1.75}
{'age': 18, 'height': 1.75}
{}
查询
- 对键遍历
- 对键和值遍历
d = {'name1' : 'pythontab', 'name2' : '.', 'name3' : 'com'}
for key in d:
print (key, ' value : ', d[key])
输出:
name1 value : pythontab
name2 value : .
name3 value : com
for key, value in d.items():
print (key, ' value : ', value)
输出:
name1 value : pythontab
name2 value : .
name3 value : com
set集合
增加
- add 添加一个字符串
- update 迭代着添加
b = {'b', 'c'} #定义集合
print(b)
a = set('boy') #定义集合
print(a)
a.add('python')
print(a)
a.update('update')
print(a)
输出:
{'b', 'c'}
{'b', 'y', 'o'}
{'b', 'python', 'y', 'o'}
{'b', 'a', 'python', 'u', 'd', 't', 'e', 'p', 'o', 'y'}
删除
- remove 按元素删除
- discard 集合的删,跟remove删是一样的,没有不会报
- pop 随机删除一个元素,有返回值
- clear 清空集合
- del 删除整个集合
a.remove('python')
print(a)
a.discard('python') #已经删除python元素了,再用discard删除不会报错
print(a)
a.pop()
print(a)
a.clear()
print(a)
del a
print(a)
输出:
{'b', 'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'}
{'b', 'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'}
{'a', 'u', 'd', 't', 'e', 'p', 'o', 'y'}
set()
NameError: name 'a' is not defined
交集【& or intersection】
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.intersection(y)
print(z)
c = x & y
print(c)
输出:
{'apple'}
{'apple'}
并集【| or union】
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.union(y)
print(z)
c = x | y
print(c)
输出:
{'cherry', 'runoob', 'google', 'banana', 'apple'}
{'cherry', 'runoob', 'google', 'banana', 'apple'}
差集【- or difference】
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)
z1 = y.difference(x)
print(z1)
c = x - y
print(c)
c1 = y - x
print(c1)
输出:
{'cherry', 'banana'}
{'google', 'microsoft'}
{'cherry', 'banana'}
{'google', 'microsoft'}
反交集【^ or symmetric_difference】
返回两个集合中不重复的元素
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
m = x ^ y
print(m)
输出:
{'microsoft', 'google', 'cherry', 'banana'}
{'microsoft', 'google', 'cherry', 'banana'}
子集【< or issubset】
a = {1,2}
b = {1,2,3,4}
print(a.issubset(b)) #a是b的子集
print(a < b)
输出:
True
True
超集【> or issuperset】
a = {1,2}
b = {1,2,3,4}
print(b.issuperset(a)) #b是a的超集
print(b > a)
输出:
True
True
frozenset 不可变集合
a = frozenset("abcd")
print(a)
a.add("d")
报错:AttributeError: 'frozenset' object has no attribute 'add'
输出:
frozenset({'b', 'a', 'd', 'c'})
Python list,tuple,dict,set高级变量常用方法的更多相关文章
- [Python笔记][第二章Python序列-tuple,dict,set]
2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...
- Python list,tuple,dict and set
list 有序可变的集合 查找和插入的时间随着元素的增加而增加 占用空间小,浪费内存很少 tuple 有序只读不可变.因为tuple不可变,所以代码更安全.如果可能,能用tuple代替list就尽量用 ...
- Python List+Tuple+Dict+Set小结
创建List:L = ['Adam', 'Lisa', 'Bart', 'Gechong', 'Kongming'] 显示List:L[0] 遍历List:print (L)和for循环 更新List ...
- list,tuple,dict,set常用方法
Python中list,tuple,dict,set常用方法 collections模块提供的其它有用扩展类型 from collections import Counter from collect ...
- python笔记:#013#高级变量类型
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- Python中的高级变量类型
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- python高级变量类型(元组,列表,字典, 字符串和重要方法)
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- Python容器--list, tuple, dict, set
## Python 中有四种用于存放数据的序列--list, tuple, dict, set ## list 列表 - 可以存放任意类型数据的有序序列 - 列表可以由零个或多个元素组成,元素之间用逗 ...
- Python中内置数据类型list,tuple,dict,set的区别和用法
Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...
随机推荐
- [2]windows内核情景分析--系统调用
Windows的地址空间分用户模式与内核模式,低2GB的部分叫用户模式,高2G的部分叫内核模式,位于用户空间的代码不能访问内核空间,位于内核空间的代码却可以访问用户空间 一个线程的运行状态分内核态与用 ...
- 内存管理4-Autoreleasepool
自动释放池是OC里面的一种内存回收机制,一般可以将一些临时变量添加到自动释放池中,统一回收释放,当自动释放池销毁时,池里面的所有对象都会调用一次release,也就是计数器会减1,但是自动释放池被销毁 ...
- c 语言延时函数
/*--- 等待x毫秒 ---*/ int sleep(unsigned long x) { clock_t c1 = clock(), c2; do { ) /* 错误 */ ; } while ( ...
- Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)
题目链接 http://codeforces.com/contest/1276/problem/C 题解 嗯,比赛结束前3min想到做法然后rush不出来了--比赛结束后又写了15min才过-- 以下 ...
- MySQL:数据库名或者数据表名包含-
[参考文章]:mysql数据库名称中包含短横线的对应方式 1. 现象 命令行下操作 名称包含 " - " 数据库或者数据表时,语句执行报错: 2. 解决方案: 使用 `` 字符(E ...
- 2018-2019-2 20175227张雪莹《Java程序设计》实验五 《网络编程与安全》
2018-2019-2 20175227张雪莹<Java程序设计> 实验五 <网络编程与安全> 实验报告封面 课程:Java程序设计 班级:1752班 姓名:张雪莹 学号:20 ...
- Python 自学笔记(二)
3-1.条件判断 3-1.条件判断 3-1-1.单项判断 if 3-1-2.双向判断 if...else... 3-1-3.多向判断 if...elif...else 3-2.if嵌套 4.输入 4- ...
- Python3中_和__的用途和区别
访问可见性问题 对于上面的代码,有C++.Java.C#等编程经验的程序员可能会问,我们给Student对象绑定的name和age属性到底具有怎样的访问权限(也称为可见性).因为在很多面向对象编程语言 ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_13.RabbitMQ研究-工作模式-header和rpc工作模式
header模式 header模式与routing不同的地方在于,header模式取消routingkey,使用header中的 key/value(键值对)匹配 队列. 案例: 根据用户的通知设置去 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_01-页面静态化需求分析
上半部分就是静态化 业务流程如下: 1.获取模型数据 2.制作模板 3.对页面进行静态化 4.将静态化生成的html页面存放文件系统中 5.将存放在文件系统的html文件发布到服务器