当前位置:网站首页>Collections and Functions
Collections and Functions
2022-08-09 09:58:00 【Wu Xizhong】
一.集合
1.集合的特点
1.Elements in a collection can only store immutable objects
2.集合中的元素是无序的
3.集合中的元素不能重复,Elements will also be automatically deduplicated if the collection is duplicated.
2.A representation of the collection
1.集合可用{a,b,c}来表示,Note that if this method is empty, it will become an empty dictionary.
2.{}Represents an empty dictionary instead of an empty collection.
3.A representation of an empty collection:
set()
3.集合的使用
1.使用方法set()可以创建集合(注意:Only iterable objects can be passed in parentheses in this method)
2.创建空集合: set()
set()
3.可通过set()Convert sequences and dictionaries to sets
4.len():通过len()Gets the number of elements in the collection
s = {
}1,2,3,4,5,6}
print(len(s))
输出结果:
6
5.add()The way is to add elements to the collection
s = {
1,2,3,4,5,6}
s.add(9)
print(s)
输出结果:
{
1, 2, 3, 4, 5, 6, 9}
6.update():将一个集合中的元素添加到另一个集合中
s = {
1,2,3,4,5,6}
s1 = {
7,8,9,10,11}
s.update(s1)
print(s)
print(s1)
输出结果:
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
{
7, 8, 9, 10, 11}
7.pop():随机删除集合中的一个元素,该方法有返回值
s = {
1,2,3,4,5,6}
r = s.pop()
print(r)
print(s)
输出结果为:
1
{
2, 3, 4, 5, 6}
8.remove():删除集合中指定的元素
s = {
1, 2, 3, 4, 5, 6,'abc'}
s.remove('abc')
print(s)
输出结果:
{
1, 2, 3, 4, 5, 6}
9.clear():清空集合
s = {
1, 2, 3, 4, 5, 6,'abc'}
s.clear()
4.集合的运算
1.& 交集运算:Find the part common to both sets,有返回值,The return value is a new collection
s = {
1, 2, 3, 4, 5, 6,'abc'}
s1= {
1, 2, 3, 4, 7,8,9,10}
r = s&s1
print(r)
输出结果:
{1, 2, 3, 4}
2.| 并集运算:Find all elements of both sets,The return value is the new collection
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4, 5, 6,'abc'}
r = s|s1
print(r)
输出结果:
{
1, 2, 3, 4, 5, 6, 'abc', 7, 8, 9, 10}
3.- 差集运算:返回值为集合
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4, 5, 6,'abc'}
r = s-s1
b = s1-s
print(r)
print(b)
输出结果:
{
5, 6, 'abc'}
{
8, 9, 10, 7}
4.^ Or set operations:The parts of two sets that do not intersect
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4, 5, 6,'abc'}
r = s^s1
print(r)
输出结果:
{
5, 6, 7, 8, 9, 10, 'abc'}
5.<= 检查一个集合是否是另一个集合的子集,运算结果为布尔值
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4,7,8,9,10}
r = s<=s1
print(r)
输出结果:
True
6.< 检查一个集合是否是另一个集合的真子集
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4,7,8}
r = s<s1
print(r)
输出结果:
True
7.>=检查一个集合是否是另一个集合的超集
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4,7,8,9,10}
r = s1>=s
print(r)
输出结果:
True
8.>检查一个集合是否是另一个集合的真超集
s1= {
1, 2, 3, 4, 7,8,9,10}
s = {
1, 2, 3, 4,7,8}
r = s1>s
print(r)
输出结果:
True
二.函数
1.函数的简介
1.函数是一个对象,函数用来保存一些可执行代码,And it can be called multiple times when needed
2.函数的语法
def 函数名(形参1,形参2,形参3·····):
代码块
注意:
Function names must conform to the specification for identifiers(可以包含字母、数字、Underscore but cannot start with a digit)
print是函数对象 print()是调用函数
3.The code in the function is not executed immediately in the program,The code inside the function will only be executed after the function is called
2.函数的参数
1.形参:形参(形式参数) 定义形参就相当于在函数内部声明了变量,但是并不是赋值
2.实参(实际参数)指定了形参,Then you must pass arguments when calling the function,实参将会赋值给对应的形参,Simply put, there are several formal parameters, and there are several actual parameters
3.How the function is passed
定义形参时,可以为形参指定默认值.指定了默认值以后,如果用户传递了参数则默认值不会生效.如果用户没有传递,则默认值就会生效
位置参数:位置参数就是将对应位置的实参赋值给对应位置的形参
关键字参数 : 关键字参数可以不按照形参定义的顺序去传递,而根据参数名进行传递
混合使用位置参数和关键字参数的时候必须将位置参数写到关键字参数前面去
3.不定长参数
1.定义函数时,可以在形参前面加一个*,这样这个形参可以获取到所有的实参,它会将所有的实参保存到一个元组中
2.带*号的形参只能有一个,可以和其他参数配合使用
3.*形参只能接受位置参数,不能接受关键字参数,*If the formal parameters are not placed at the end,Then the following formal parameters must be specified with keywords.
def fn(b,*c):
print(b)
print(c)
fn(1,4,6,7,8)
输出结果:
1
(4, 6, 7, 8)
4.**形参可以接收其他的关键字参数,它会将这些参数统一保存到字典当中.字典的key就是参数的名字,字典的value就是参数的值
5.**形参只有一个,并且必须写在所有参数的后面
4.参数的解包
1.传递实参时,也可以在序列类型的参数前添加星号,这样它会自动的将序列中元素依次作为参数传递
2.要求序列中的元素的个数必须和形参的个数一致
def fn(a,b,c,d):
return a+b+c+d
r = (1,2,3,4)
j = fn(*r)
print(j)
输出结果:
10
**Unpacking of formal parameters
def fn(a,b,c):
print('a=',a)
print('b=',b)
print('c=',c)
d = {
'a':'葫芦娃','b':18,'c':'female'}
fn(**d)
输出结果:
a= 葫芦娃
b= 18
c= female
边栏推荐
猜你喜欢
随机推荐
归并排序
集合与函数
KMP& sunday
Win系统 - 罗技 G604 鼠标蓝灯闪烁、失灵解决方案
[ASM] Bytecode operation MethodVisitor case combat generation object
类 对象 属性 方法 类的成员
Super detailed MySQL basic operations
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
【八大排序①】插入排序(直接插入排序、希尔排序)
批量修改Shapefile属性表的一种方法(使用gdal.jar)
5.转换流
【size_t是无符号整数 (-1 > 10) -> 1】
STM32F103实现IAP在线升级应用程序
8.Properties property collection
cannot import name ‘load_offloaded_weights‘ from ‘accelerate.utils‘ (/home/huhao/anaconda3/envs/huha
Tigase插件编写——注册用户批量查询
2.Collection interface
MySQL关于表的知识点,进来学习!
mac 上安装Redis和配置
Go-控制语句那些事