【Python基础】详解Python基础函数,包教包会(下)
运行环境
python:3.8.3
jupyter-notebook : 6.4.0
注意:本文案例可以直接在 jupyter-notebook 上运行,但在 PyCharm 上的话需要代码的最后一句加上 print 哦!
元组
tuple()
tuple() 将可迭代对象转换成元组。
tuple([0,1,2]) + tuple(range(3)) + tuple({0,1,2}) + tuple('012')
| output:(0, 1, 2, 0, 1, 2, 0, 1, 2, ‘0’, ‘1’, ‘2’) |
|---|
将可迭代对象转换成列表并通过运算符连接。
字典
dict.clear()
clear() 清除字典的所有内容。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.clear()
dic
| output:{} |
|---|
dict.fromkeys()
fromkeys() 创建一个新字典,以序列 iterable 中元素做字典的键,value 为字典所有键对应的初始值。
- iterable: 可迭代对象,新字典的键。
- value: 可选参数, 设置键序列对应的值,默认为
None。
dict.fromkeys(['CSDN', '公众号'],
'Python新视野')
| output:{‘CSDN’: ‘Python新视野’, ‘公众号’: ‘Python新视野’} |
|---|
dict.get()
get(key, default=None) 根据指定的 key 值查找,如果 key 在字典中,则返回 key 的值,否则为 None。
示例 :a:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.get('CSDN')
| output:‘Dream丶killer’ |
|---|
示例 :b:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
print(dic.get('微信'))
| output:None |
|---|
字典中没有 key 等于 '微信',返回 None,jupyter notebook 对于 None 如果不加 print 默认不输出,所以这里加上print 来打印结果
dict.items()
items() 返回视图对象,是一个可遍历的 key/value 对,可以使用 list() 将其转换为列表。
示例 :a:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
list(dic.items())
| output:[(‘CSDN’, ‘Dream丶killer’), (‘公众号’, ‘Python新视野’)] |
|---|
示例 :b:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
for key, value in dic.items():
print('key: ', key, 'value: ', value)
# key: CSDN value: Dream丶killer
# key: 公众号 value: Python新视野
dict.keys()
keys() 返回一个视图对象,值为字典的 key ,可将其转换成列表。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.keys()
| output:dict_keys([‘CSDN’, ‘公众号’]) |
|---|
dict.setdefault()
setdefault(key, default=None) 如果键不在字典中,则插入值为 None 的键。如果键在字典中,则返回键的值。
示例 :a:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.setdefault('CSDN', 'python-sun')
| output:‘Dream丶killer’ |
|---|
字典中有 CSDN 这个 key 值,返回 CSDN 对应的值,不需要插入
示例 :b:
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.setdefault('微信', 'python-sun')
dic
| output:{‘CSDN’: ‘Dream丶killer’, ‘公众号’: ‘Python新视野’, ‘微信’: ‘python-sun’} |
|---|
字典中没有 微信 这个 key 值,返回 None ,执行插入,并根据设置的参数 python-sun 来进行赋值。
dict.update()
dict.update(dict1) 把字典 dict1 的 key/value 对更新到 dict 里,当 dict1 的 key 出现在 dict 中则修改 dict 中的值,如果 key 没有出现在 dict 中,则添加这一对 key/value 。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic1 = {'CSDN': 'new_name',
'微信': 'python-sun'}
dic.update(dic1)
dic
| output:{‘CSDN’: ‘new_name’, ‘公众号’: ‘Python新视野’, ‘微信’: ‘python-sun’} |
|---|
dict.values()
values() 返回一个视图对象,值为字典的 value ,可将其转换成列表。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.values()
| output:dict_values([‘Dream丶killer’, ‘Python新视野’]) |
|---|
dict.pop()
pop() 删除指定 key 的 key/value ,如果 key 没有找到,则报错。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.pop('CSDN')
dic
| output:{‘公众号’: ‘Python新视野’} |
|---|
dict.popitem()
popitem() 删除字典中末尾的元素,并返回一个元组的(键,值)对。字典为空则报错。
dic = {'CSDN': 'Dream丶killer',
'公众号': 'Python新视野'}
dic.popitem()
| output:(‘公众号’, ‘Python新视野’) |
|---|
集合
set.add()
向集合中添加一个元素,但如果该元素已经出现在集合中,则不起作用。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.add('python')
set1
| output:{‘Dream丶Killer’, ‘Python新视野’, ‘python’, ‘python-sun’} |
|---|
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.add('python-sun')
set1
| output:{‘Dream丶Killer’, ‘Python新视野’, ‘python-sun’} |
|---|
添加的元素 python-sun 已经出现在集合中,所以 set1 不发生变化
set.clear()
clear() 移除集合中的所有元素。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.clear()
set1
| output:set() |
|---|
set.difference() & set.difference_update()
:one: difference() 返回多个集合的差集,通俗来讲就是返回第一个 set 中哪些元素没有在其他 set 中出现。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python', 'Dream丶Killer'}
set3 = {'Python新视野'}
set1.difference(set2, set3)
| output:{‘python-sun’} |
|---|
set1 中的元素只有 python-sun 没有在 set2 与 set3 中出现过,所以以及集合的形式返回 {'python-sun'}
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python', 'Dream丶Killer'}
set3 = {'Python新视野', 'python-sun'}
set1.difference(set2, set3)
| output:set() |
|---|
set1 中的元素都在 set2 与 set3 中出现过,所以返回空集合 set()
:two: difference_update() 方法与 difference() 方法的区别在于 difference() 方法返回一个移除相同元素的新集合,而 difference_update() 方法是直接移除原集合中的元素,无返回值。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python', 'Dream丶Killer'}
set3 = {'Python新视野'}
set1.difference_update(set2, set3)
set1
| output:{‘python-sun’} |
|---|
set1 中的元素 Dream丶Killer 与 Python新视野 都有在 set2 与 set3 中出现过,所以从 set1 中移除这些值
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python', 'Dream丶Killer'}
set3 = {'Python新视野', 'python-sun'}
set1.difference_update(set2, set3)
set1
| output:set() |
|---|
set1 中的元素都在 set2 与 set3 中出现过,set1 移除所有值后为空集合 set()
set.discard()
discard() 删除集合中指定的元素。如果指定移除的元素不在集合中,则不移除。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.discard('Dream丶Killer')
set1
| output:{‘Python新视野’, ‘python-sun’} |
|---|
指定的元素存在于 set1 ,将它存 set1 中移除
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.discard('python')
set1
| output:{‘Dream丶Killer’, ‘Python新视野’, ‘python-sun’} |
|---|
指定的元素不在 set1 内,set1 不做改变
set.intersection() & set.intersection_update()
:one: intersection() 返回集合的交集。没有交集则返回空集 set() 。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python-sun', 'Dream丶Killer'}
set3 = {'Python新视野', 'python-sun'}
set1.intersection(set2, set3)
| output:{‘python-sun’} |
|---|
返回 set1、set2、set3 中同时出现的元素
:two: intersection_update() 方法与 intersection() 方法的区别在于 intersection() 方法将集合的交集作为新集合返回,而 intersection_update() 方法是直接修改原集合中的元素,只保留交集元素,无返回值。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python-sun', 'Dream丶Killer'}
set3 = {'Python新视野', 'python-sun'}
set1.intersection_update(set2, set3)
set1
| output:{‘python-sun’} |
|---|
set1 中只有 'python-sun 同时在 set2 与 set3 中出现过,因此移除 set1 中其他元素
set.isdisjoint()
isdisjoint() 判断两个集合是否包含相同的元素,有则返回 False,无则返回 True。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python-sun', 'Dream丶Killer'}
set1.isdisjoint(set2)
| output:False |
|---|
set1 与 set2 中有两个相同元素,返回 False
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python'}
set1.isdisjoint(set2)
| output:True |
|---|
set1 与 set2 中无相同元素,返回 True
set.issubset()
set2.issubset(set1) 判断集合 set2 是否为 set1 集合的子集。是则返回 True,否则返回 False。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python-sun', 'Dream丶Killer'}
set2.issubset(set1)
| output:True |
|---|
set2 是 set1 集合的子集,故返回 True ,使用时需注意 set1 与 set2 的位置顺序。如果写成 set1.issubset(set2) 则会返回 False
set.issuperset()
set1.issuperset(set2) 判断集合 set2 是否为 set1 集合的子集。是则返回 True,否则返回 False。它与 issubset() 用法相同,只有参数的位置相反而已。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python-sun', 'Dream丶Killer'}
set1.issuperset(set2)
| output:True |
|---|
set1.pop()
pop() 移除并返回集合中的任意元素。如果该集合为空集则报错。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.pop()
| output:‘python-sun’ |
|---|
set.remove()
remove() 从集合中移除指定的元素,如果该元素不在集合中,则发生报错。
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.remove('Dream丶Killer')
set1
| output:{‘Python新视野’, ‘python-sun’} |
|---|
set.symmetric_difference()
symmetric_difference() 返回两个集合中不重复的元素集合,即两个集合的补集,与 ^ 的作用相同。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python', 'python-sun', 'Dream丶Killer'}
set1.symmetric_difference(set2)
| output:{‘Python新视野’, ‘python’} |
|---|
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python',
'python-sun',
'Dream丶Killer'}
set1 ^ set2
| output:{‘Python新视野’, ‘python’} |
|---|
结果与上面相同
set.symmetric_difference_update()
set1.symmetric_difference_update(set2) 移除 set1 中在 set2 相同的元素,并将 set2 集合中不同的元素插入到 set1 中。简单来说就是把 set1 与 set2 的补集赋值给 set1。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python',
'python-sun',
'Dream丶Killer'}
set1.symmetric_difference_update(set2)
set1
| output:{‘Python新视野’, ‘python’} |
|---|
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python',
'python-sun',
'Dream丶Killer'}
set1 = set1 ^ set2
set1
| output:{‘Python新视野’, ‘python’} |
|---|
set.union()
union() 返回多个集合的并集。与 | 的作用相同。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python',
'python-sun',
'Dream丶Killer'}
set3 = {'ABC'}
set1.union(set2, set3)
| output:{‘ABC’, ‘Dream丶Killer’, ‘Python新视野’, ‘python’, ‘python-sun’} |
|---|
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set2 = {'python',
'python-sun',
'Dream丶Killer'}
set3 = {'ABC'}
set1 | set2 | set3
| output:{‘ABC’, ‘Dream丶Killer’, ‘Python新视野’, ‘python’, ‘python-sun’} |
|---|
set.update()
update() 使用本身和其他的联合来更新集合。
示例 :a:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1.update([1,2,3])
set1
| output:{1, 2, 3, ‘Dream丶Killer’, ‘Python新视野’, ‘python-sun’} |
|---|
示例 :b:
set1 = {'Dream丶Killer',
'Python新视野',
'python-sun'}
set1 = set1 | set([1,2,3])
set1
| output:{1, 2, 3, ‘Dream丶Killer’, ‘Python新视野’, ‘python-sun’} |
|---|
使用 | 也可以达到相同的效果。
对于刚入门 Python 或是想要入门 Python 的小伙伴,可以通微信搜Python新视野,一起交流学习,都是从新手走过来的,有时候一个简单的问题卡很久,但可能别人的一点拨就会恍然大悟,由衷的希望大家能够共同进步。
- 点赞
- 收藏
- 关注作者
评论(0)