Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

Python数据类型内置函数

  - str(字符串)

  - list(列表)

  - tuple(元组)

  - dict(字典)

  - set(收集)


 

tuple(元组)的操作

  

- (count)统计元组中元素出现的次数,返回统计值 

1 # 统计元组中指定元素出现的次数,返回出现次数的值
2 tpe_1 = (2,3,4,2,5,6,2,7)
3 tpe_2 = tpe_1.count(2)
4 
5 print(tpe_2)
6 # 执行结果
7 3

 

 

- (index)指定元组的值找出它的索引,返回索引的值

 1 # 找出元组中指定的值的索引,返回索引
 2 tpe_1 = (3,5,1,7,4,1)
 3 tpe_2 = tpe_1.index(1)
 4 tpe_3 = tpe_1.index(1,3,6)
 5 
 6 print(tpe_2)
 7 # 执行结果
 8 2
 9 
10 print(tpe_3)
11 # 执行结果
12 5

 


 

dict(字典)的操作

  

- (clear)对字典中的内容进行清除,返回None

 1 # 对字典中的内容进行清除,返回None
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.clear()
 4 
 5 print(dct_1)
 6 # 执行结果
 7 {}
 8 
 9 print(dct_2)
10 # 执行结果
11 None

 

- (copy)创建一块新的内存地址进行赋值,返回集合

 1 # 对字典进行拷贝,创建一个新的地址内存进行赋值
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 
 4 print(id(dct_1))
 5 # 执行结果
 6 3084069826424
 7 
 8 dct_2 = dct_1.copy()
 9 
10 print(id(dct_2))
11 # 执行结果
12 3084069854448
13 
14 print(dct_2)
15 # 执行结果
16 {'a': 1, 'b': 2, 'c': 3}

- (get)指定字典的键,返回它的值,如果没有这个键返回None或你设置了它的值,则返回值

 1 # 指定字典的键,返回值
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.get('a')
 4 dct_3 = dct_1.get('d',4)
 5 dct_4 = dct_1.get('d')
 6 
 7 print(dct_2)
 8 # 执行结果
 9 1
10 
11 print(dct_3)
12 # 执行结果
13 4
14 
15 print(dct_4)
16 # 执行结果
17 None

 

- (items)使字典以元组的形式遍历

 1 # 将字典转化为元组形式遍历
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.items()
 4 
 5 print(dct_1)
 6 # 执行结果
 7 {'a': 1, 'b': 2, 'c': 3}
 8 
 9 print(dct_2)
10 # 执行结果
11 dict_items([('a', 1), ('b', 2), ('c', 3)])
12 
13 # 直接循环打印出键和值
14 for k,v in dct_1.items():
15     print(k,v)
16 #执行结果
17 a 1
18 b 2
19 c 3
20 
21 # 以元组形式打印出键和值
22 for i in dct_1.items():
23     print(i)
24 #执行结果
25 ('a', 1)
26 ('b', 2)
27 ('c', 3)

 

- (keys)字典中所有的键返回

1 # 字典中所有的键返回
2 dct_1 = {'a':1,'b':2,'c':3}
3 dct_2 = dct_1.keys()
4 
5 print(dct_2)
6 # 执行结果
7 dict_keys(['a', 'b', 'c'])

 

- (pop)删除一个键返回他值,如果没有这个键就要设置它的值,返回它的值,如果不设置报错

 1 # 删除一个键,返回这个键的值也可以字设置值,如果没有这个键必须设置值,如果没有,默认报错
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.pop('a')
 4 
 5 print(dct_2)
 6 # 执行结果
 7 1
 8 
 9 print(dct_1)
10 # 执行结果
11 {'b': 2, 'c': 3}
12 
13 #dct_3 = dct_1.pop('a')
14 dct_4 = dct_1.pop('a',6)
15 
16 print(dct_3)
17 #执行结果
18 KeyError
19 
20 print(dct_4)
21 # 执行结果
22 6
23 
24 dct_5 = dct_1.pop('d',5)
25 
26 print(dct_5)
27 # 执行结果
28 5

 

- (popitem)删除字典最后一对键值,返回删除的键值对以元组形式的返回

1 # 删除字典中的键值,以元组形式返回键值
2 dct_1 = {'a':1,'b':2,'c':3}
3 dct_2 = dct_1.popitem()
4 
5 print(dct_2)
6 # 执行结果
7 ('c', 3)

 

-  新增字典的键和值,返回值,如果只设置键没有设定值,则返回None

 1 # 在字典中新增的键值,返回值,如果没有设键,返回None
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.setdefault('d',4)
 4 dct_3 = dct_1.setdefault('e')
 5 
 6 print(dct_1)
 7 # 执行结果
 8 {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': None}
 9 
10 print(dct_2)
11 # 执行结果
12 4
13 
14 print(dct_3)
15 # 执行结果
16 None

 

- (update)修改字典中的键值,返回None

 1 # 修改字典中的键和值,返回None
 2 dct_1 = {'a':1,'b':2,'c':3}
 3 dct_2 = dct_1.update({'a':9,'b':6,'c':2,'d':4})
 4 
 5 print(dct_1)
 6 # 执行结果
 7 {'a': 9, 'b': 6, 'c': 2, 'd': 4}
 8 
 9 print(dct_2)
10 # 执行结果
11 None

 

- (values)返回字典中所有的值

1 # 返回字典中所有的值
2 dct_1 = {'a':1,'b':2,'c':3}
3 dct_2 = dct_1.values()
4 
5 print(dct_2)
6 # 执行结果
7 dict_values([1, 2, 3])

 


 

set(集合)的操作  

 

- (add)在集合末尾添加新的元素,返回None

 1 # 在集合内添加元素,如果添加重复则不显示
 2 st_1 = {1,2,3,4,5}
 3 st_2 = st_1.add(0)
 4 st_3 = st_1.add(9)
 5 
 6 print(st)
 7 # 执行结果
 8 {0, 1, 2, 3, 4, 5, 9}
 9 
10 print(st_2)
11 # 执行结果
12 None

 

-  (clear)集合内的元素全部清除,返回None

 1 # 清除集合内的元素,返回None
 2 st_1 = {"one","strip",9}
 3 st_2 = st_1.clear()
 4 
 5 print(st_1)
 6 # 执行结果
 7 set()
 8 
 9 print(st_2)
10 # 执行结果
11 None

 

- (copy)创建一个新的内存地址进行赋值,返回集合

1 # 复制一个集合创意新的内存地址,返回集合
2 st_1 = {"practice","default"}
3 st_2 = st_1.copy()
4 
5 print(st_2)
6 # 执行结果
7 {'practice', 'default'}

 

- (difference)集合差集,返回集合一在集合二中不一致的元素

 1 # 集合中的差集,返回集合一存在集合二不存在的元素
 2 st_1 = {"services","practice","strip","fast"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.difference(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'services', 'fast', 'strip', 'practice'}
 9 
10 print(st_3)
11 # 执行结果
12 {'fast', 'strip'}

  

- (difference_update)集合差集,删除集合一在集合二中不一致的元素,返回None

 1 # 集合中的差集,返回集合一在集合二中不一致的元素,返回None
 2 st_1 = {"services","practice","strip","fast"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.difference_update(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'fast', 'strip'}
 9 
10 print(st_3)
11 # 执行结果
12 None

 

-  (discard)删除集合中指定的元素,返回None

 1 # 删除集合中指定的元素,返回None
 2 st_1 = {28,36,21,"fast","split"}
 3 st_2 = st_1.discard(21)
 4 
 5 print(st_1)
 6 # 执行结果
 7 {36, 'fast', 'split', 28}
 8 
 9 st_3 = st_1.discard("fast")
10 
11 print(st_1)
12 # 执行结果
13 {36, 'split', 28}
14 
15 print(st_2)
16 #执行结果
17 None

 

- (intersection)交叉,返回集合中共有的元素

 1 # 交叉,返回集合中共有的元素
 2 st_1 = {"services","practice","strip","fast","over"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.intersection(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'services', 'over', 'practice', 'fast', 'strip'}
 9 
10 print(st_3)
11 # 执行结果
12 {'services', 'over', 'practice'}

 

- (intersection_update)交叉集合中共有的元素,返回None

 1 # 交叉集合中共有的元素,返回None 
 2 st_1 = {"services","practice","strip","fast","over"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.intersection_update(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'services', 'over', 'practice'}
 9 
10 print(st_3)
11 # 执行结果
12 None

 

- (isdisjoint)两个集合交叉,如果没有共同的元素返回Ture,有共同元素返回False

 1 # 两个集合具有空交集,没有返回Ture,有相同元素返回False
 2 st_1 = {"services","practice","strip","fast","over"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.isdisjoint(st_2)
 5 
 6 print(st_3)
 7 # 执行结果
 8 False
 9 
10 st_4 = {99,"difference"}
11 st_5 = {"analysis","design","Implementation"}
12 st_6 = st_4.isdisjoint(st_5)
13 
14 print(st_6)
15 # 执行结果
16 Ture

 

- (issubset)判断集合一中的所有元素是否在指定集合内,返回布尔值

 1 #  判断集合1中所有元素是否包含在指定集合中,如果是返回Ture,否则返回False
 2 st_1 = {1,2,3,4,5}
 3 st_2 = {1,2,3,4,5,6,7,8}
 4 st_3 = st_1.issubset(st_2)
 5 
 6 print(st_3)
 7 # 执行结果
 8 Ture
 9 
10 st_4 = {1,2,3,4,5}
11 st_5 = {1,2,3,6,7,8}
12 st_6 = st_4.issubset(st_5)
13 print(st_6)
14 # 执行结果
15 False

 

- (issuperset)判断指定集合有没有在集合一中,返回布尔值

# 判断集合中指定的集合是否在集合1中,返回布尔值
st_1 = {1,2,3,4,5}
st_2 = {1,2,3}
st_3 = st_1.issuperset(st_2)

print(st_3)
# 执行结果
Ture

st_4 = {1,2,3,4,5}
st_5 = {1,2,3,6}
st_6 = st_4.issuperset(st_5)

print(st_6)
# 执行结果
False

 

- (pop)随机删除指定集合内的元素,返回值

 1 # 随机删除集合中一个元素,返回删除元素
 2 st_1 = {"super","subset","isdisjoint"}
 3 st_2 = st_1.pop()
 4 
 5 print(st_1)
 6 # 执行结果
 7 {'subset', 'spuer'}
 8 
 9 print(st_2)
10 # 执行结果
11 isdisjoint

 

- (remove)删除集合内指定的成员,返回None

 1 # 指定删除一个集合中的成员,返回None
 2 st_1 = {"super","subset","isdisjoint"}
 3 st_2 = st_1.remove("isdisjoint")
 4 
 5 print(st_1)
 6 # 执行结果
 7 {'subset', 'super'}
 8 
 9 print(st_2)
10 # 执行结果
11 None

 

- (symmetric_difference)返回差集中不重复的元素

1 # 指定集合,返回两个集合中不重复的元素,将重复的删除
2 st_1 = {"services","practice","strip","fast","over"}
3 st_2 = {"split","practice","services","over"}
4 st_3 = st_1.symmetric_difference(st_2)
5 
6 
7 print(st_3)
8 # 执行结果
9 {'split', 'fast', 'strip'}

 

- (symmetric_difference)差集中不重复的元素,返回None

 1 # 两个集合中不重复的元素,返回None
 2 st_1 = {"services","practice","strip","fast","over"}
 3 st_2 = {"split","practice","services","over"}
 4 st_3 = st_1.symmetric_difference_update(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'fast', 'strip', 'split'}
 9 
10 print(st_3)
11 # 执行结果
12 None

 

 

- (union)合并两个集合将重复的元素删除,返回集合

1 # 合并两个集合重复的删除,返回并集
2 st_1 = {"union","analysis","design"}
3 st_2 = {"cherry","union","design","analysis"}
4 st_3 = st_1.union(st_2)
5 
6 
7 print(st_3)
8 # 执行结果
9 {'design', 'analysis', 'cherry', 'union'}

 

- (union_update)将两个集合重复的元素删除,返回None

 1 # 合并两个集合重复的删除,返回None
 2 st_1 = {"union","analysis","design"}
 3 st_2 = {"cherry","union","design","analysis"}
 4 st_3 = st_1.update(st_2)
 5 
 6 print(st_1)
 7 # 执行结果
 8 {'design', 'analysis', 'cherry', 'union'}
 9 
10 print(st_3)
11 # 执行结果
12 None

 

转载于:https://www.cnblogs.com/Rimmpeddo/p/10141849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值