Python基础篇【第五篇】:sed和函数

sed是一个无序且不重复的元素集合

  1 class set(object):
  2     """
  3     set() -> new empty set object
  4     set(iterable) -> new set object
  5
  6     Build an unordered collection of unique elements.
  7     """
  8     def add(self, *args, **kwargs): # real signature unknown
  9         """
 10         ‘‘‘添加元素‘‘‘
 11         Add an element to a set.
 12
 13         This has no effect if the element is already present.
 14         """
 15         pass
 16
 17     def clear(self, *args, **kwargs): # real signature unknown
 18         ‘‘‘清除内容(set中的元素)‘‘‘
 19         """ Remove all elements from this set. """
 20         pass
 21
 22     def copy(self, *args, **kwargs): # real signature unknown
 23         ‘‘‘浅拷贝‘‘‘
 24         """ Return a shallow copy of a set. """
 25         pass
 26
 27     def difference(self, *args, **kwargs): # real signature unknown
 28         ‘‘‘A中存在B中不存在‘‘‘
 29         """
 30         Return the difference of two or more sets as a new set.
 31
 32         (i.e. all elements that are in this set but not the others.)
 33         """
 34         pass
 35
 36     def difference_update(self, *args, **kwargs): # real signature unknown
 37         ‘‘‘从当前的集合中删除B中相同的元素‘‘‘
 38         """ Remove all elements of another set from this set. """
 39         pass
 40
 41     def discard(self, *args, **kwargs): # real signature unknown
 42         ‘‘‘移除元素,不存在不报错‘‘‘
 43         """
 44         Remove an element from a set if it is a member.
 45
 46         If the element is not a member, do nothing.
 47         """
 48         pass
 49
 50     def intersection(self, *args, **kwargs): # real signature unknown
 51         ‘‘‘交集‘‘‘
 52         """
 53         Return the intersection of two sets as a new set.
 54
 55         (i.e. all elements that are in both sets.)
 56         """
 57         pass
 58
 59     def intersection_update(self, *args, **kwargs): # real signature unknown
 60         ‘‘‘取交集并更新到A中‘‘‘
 61         """ Update a set with the intersection of itself and another. """
 62         pass
 63
 64     def isdisjoint(self, *args, **kwargs): # real signature unknown
 65         ‘‘‘如果没有交集返回true,否则返回false‘‘‘
 66         """ Return True if two sets have a null intersection. """
 67         pass
 68
 69     def issubset(self, *args, **kwargs): # real signature unknown
 70         ‘‘‘是否是子序列‘‘‘
 71         """ Report whether another set contains this set. """
 72         pass
 73
 74     def issuperset(self, *args, **kwargs): # real signature unknown
 75         ‘‘‘是否是父序列‘‘‘
 76         """ Report whether this set contains another set. """
 77         pass
 78
 79     def pop(self, *args, **kwargs): # real signature unknown
 80         ‘‘‘移除元素,并拿到该元素‘‘‘
 81         """
 82         Remove and return an arbitrary set element.
 83         Raises KeyError if the set is empty.
 84         """
 85         pass
 86
 87     def remove(self, *args, **kwargs): # real signature unknown
 88         ‘‘‘移除指定元素,不存在报错‘‘‘
 89         """
 90         Remove an element from a set; it must be a member.
 91
 92         If the element is not a member, raise a KeyError.
 93         """
 94         pass
 95
 96     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 97         ‘‘‘对称交集‘‘‘
 98         """
 99         Return the symmetric difference of two sets as a new set.
100
101         (i.e. all elements that are in exactly one of the sets.)
102         """
103         pass
104
105     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
106         ‘‘‘对称交集,并更新到a中‘‘‘
107         """ Update a set with the symmetric difference of itself and another. """
108         pass
109
110     def union(self, *args, **kwargs): # real signature unknown
111         ‘‘‘并集‘‘‘
112         """
113         Return the union of sets as a new set.
114
115         (i.e. all elements that are in either set.)
116         """
117         pass
118
119     def update(self, *args, **kwargs): # real signature unknown
120         ‘‘‘更新‘‘‘
121         """ Update a set with the union of itself and others. """
122         pass
123
124     def __and__(self, *args, **kwargs): # real signature unknown
125         """ Return self&value. """
126         pass
127
128     def __contains__(self, y): # real signature unknown; restored from __doc__
129         """ x.__contains__(y) <==> y in x. """
130         pass
131
132     def __eq__(self, *args, **kwargs): # real signature unknown
133         """ Return self==value. """
134         pass
135
136     def __getattribute__(self, *args, **kwargs): # real signature unknown
137         """ Return getattr(self, name). """
138         pass
139
140     def __ge__(self, *args, **kwargs): # real signature unknown
141         """ Return self>=value. """
142         pass
143
144     def __gt__(self, *args, **kwargs): # real signature unknown
145         """ Return self>value. """
146         pass
147
148     def __iand__(self, *args, **kwargs): # real signature unknown
149         """ Return self&=value. """
150         pass
151
152     def __init__(self, seq=()): # known special case of set.__init__
153         """
154         set() -> new empty set object
155         set(iterable) -> new set object
156
157         Build an unordered collection of unique elements.
158         # (copied from class doc)
159         """
160         pass
161
162     def __ior__(self, *args, **kwargs): # real signature unknown
163         """ Return self|=value. """
164         pass
165
166     def __isub__(self, *args, **kwargs): # real signature unknown
167         """ Return self-=value. """
168         pass
169
170     def __iter__(self, *args, **kwargs): # real signature unknown
171         """ Implement iter(self). """
172         pass
173
174     def __ixor__(self, *args, **kwargs): # real signature unknown
175         """ Return self^=value. """
176         pass
177
178     def __len__(self, *args, **kwargs): # real signature unknown
179         """ Return len(self). """
180         pass
181
182     def __le__(self, *args, **kwargs): # real signature unknown
183         """ Return self<=value. """
184         pass
185
186     def __lt__(self, *args, **kwargs): # real signature unknown
187         """ Return self<value. """
188         pass
189
190     @staticmethod # known case of __new__
191     def __new__(*args, **kwargs): # real signature unknown
192         """ Create and return a new object.  See help(type) for accurate signature. """
193         pass
194
195     def __ne__(self, *args, **kwargs): # real signature unknown
196         """ Return self!=value. """
197         pass
198
199     def __or__(self, *args, **kwargs): # real signature unknown
200         """ Return self|value. """
201         pass
202
203     def __rand__(self, *args, **kwargs): # real signature unknown
204         """ Return value&self. """
205         pass
206
207     def __reduce__(self, *args, **kwargs): # real signature unknown
208         """ Return state information for pickling. """
209         pass
210
211     def __repr__(self, *args, **kwargs): # real signature unknown
212         """ Return repr(self). """
213         pass
214
215     def __ror__(self, *args, **kwargs): # real signature unknown
216         """ Return value|self. """
217         pass
218
219     def __rsub__(self, *args, **kwargs): # real signature unknown
220         """ Return value-self. """
221         pass
222
223     def __rxor__(self, *args, **kwargs): # real signature unknown
224         """ Return value^self. """
225         pass
226
227     def __sizeof__(self): # real signature unknown; restored from __doc__
228         """ S.__sizeof__() -> size of S in memory, in bytes """
229         pass
230
231     def __sub__(self, *args, **kwargs): # real signature unknown
232         """ Return self-value. """
233         pass
234
235     def __xor__(self, *args, **kwargs): # real signature unknown
236         """ Return self^value. """
237         pass
238
239     __hash__ = None

以上常用实例:

 1 #!/usr/bin/env python3
 2
 3 # se = {11,22,33}
 4 # be = {55}
 5
 6 # #增加
 7 # se.add(44)
 8 # print(se)
 9
10 # 更新
11 # se.update(be)
12 # print(se)
13
14 # #清除
15 # se.clear()
16 # print(se)
17
18 # #找se中存在的be不存在的
19 # se.difference(be)
20 # ret = se.difference(be)
21 # print(se)
22 # print(ret)
23
24
25 # #找se中存在,be中不存在的集合,更新自己
26 # se.difference_update(be)
27 # print(se)
28
29 # #移除指定的元素,不存在不报错
30 # se.discard(11)
31 # print(se)
32
33 # #移除元素,不存在会报错
34 # se.remove(11)
35 # print(se)
36
37 # #取交集
38 # ret = se.intersection(be)
39 # print(ret)
40
41 # #取交集并更新
42 # se.intersection_update(be)
43 # print(se)
44
45 # #判断交集有false,没有true
46 # ret = se.isdisjoint(be)
47 # print(ret)
48
49 #子序列,父序列,谁多谁是父亲,谁少谁是儿子
50 # ret = se.issubset(be)
51 # print(ret)
52 # ret = se.issuperset(be)
53 # print(ret)
54
55 #移除
56 # ret = se.pop()
57 # print(ret)
58
59 #对称交集
60 # set = se.symmetric_difference(be)
61 # print(set)
62
63 # #对称交集更新
64 # se.symmetric_difference_update(be)
65 # print(se)
66
67 #并集
68 # ret = se.union(be)
69 # print(ret)
70
71 # se = {11,22,33}
72 # be = {22,55}
73 # for i in se:
74 #     print(se)
75 #
76 # ret = 11 in se
77 # print(ret)

深、浅copy

1、深拷贝是在另一块地址中创建一个新的变量或容器,同时容器内的元素的地址也是新开辟的,仅仅是值相同而已,是完全的副本。

2、浅拷贝是在另一块地址中创建一个新的变量或容器,但是容器内的元素的地址均是源对象的元素的地址的拷贝。也就是说新的容器中指向了旧的元素。

函数

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。

函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。

定义函数: 

  • 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ()
  • 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。
  • 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
  • 函数内容以冒号起始,并且缩进。
  • return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。

语法:

1 def 函数名字(形参):
2     函数体

默认情况下,参数值和参数名称是按函数声明中定义的的顺序匹配起来的。

实例:

1 def hello():
2     print(‘hello~‘)

调用函数:

1 #!/usr/bin/env python3
2
3 def test(str):
4     print(str);
5     return;
6
7 printme("调用")
时间: 2024-08-28 05:31:12

Python基础篇【第五篇】:sed和函数的相关文章

Python 基础【第五篇】元组和列表

一 .Python之列表: 其实所谓的列表我个人感觉和shell 中的数组是一样的(只是个人见解哦),列表其实说白了就是元素的组合: 格式: Name = [a,b,c,d] 下标: 每一个列表中的元素都对应一个下标下标的起始位为0 比如列表[a,b,c,d] a对应的下标为0 b对应的下标为1 c对应的下标为2 …… 1.1.定义列表: 比如定义列表group 元素分别为(test,11,bbb,343,ccc) >>> group = ["test",11,&qu

python学习记录第五篇--遍历目录

#coding=utf-8'''@author: 简单遍历目录删除文件的小程序'''import os#查找文件操作def findFile(path): fileList=[] for rootPath,subRoot,fileName in os.walk(path): for sub in fileName: if os.path.isfile(os.path.join(rootPath,sub)): k=os.path.splitext(sub)[1].lower() if k in (

Python基础知识(五)

# -*- coding: utf-8 -*-# @Time : 2018-12-25 19:31# @Author : 三斤春药# @Email : [email protected]# @File : Python基础知识(五).py# @Software : PyCharm Python基础知识(五)今日内容大纲:昨日内容回顾 list: 增:append() insert(index,object) extend() 迭代着追加 删: pop 默认删除最后一个,按照索引去删除,有返回值.

python基础----迭代器、生成器、协程函数

一.什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代对象:实现了迭代器协议的对象(如何实现:对象内部定义一个__iter__()方法) 3.协议是一种约定,可迭代对象实现了迭代器协议,python的内部工具(如for循环,sum,min,max函数等)使用迭代器协议访问对象. 二,为什么要用迭代器 优点: 1:迭代器提供了一种不依赖于索引的取值方式,

python基础 带参数以及返回值的函数装饰器

1 #带参数以及返回值的函数装饰器,上一篇博客记录了无参数函数装饰器写法以及使用方案,当函数有参数以及返回值时需要将装饰器进行如下修稿 2 def timer(fun): 3 def deco(*args,**kwargs): #被装饰函数实际是执行deco,所以在此将被装饰函数参数进行传递 4 start_time = time.time() 5 res = fun(*args,**kwargs) #将被装饰函数的返回值接收 6 stop_time = time.time() 7 return

python基础之名称空间和作用域、函数嵌套

一.名称空间 1.定义:存放名字与值的绑定关系 2.名称空间分为:内置名称空间.全局名称空间.局部名称空间 内置名称空间:python解释器自带的名字,python解释器启动就会生成 全局名称空间:文件级别定义的名字都会存放与全局名称空间,执行python文件时会产生 局部名称空间:定义在函数内部的名字,局部名称空间只有在调用函数时才会生效,函数调用结束则失效 3.加载的顺序:三者的加载顺序:内置名称空间->全局名称空间->局部名称空间 4.取值的顺序:局部名称空间->全局名称空间-&g

python基础(7)--深浅拷贝、函数

1.深浅拷贝 在Python中将一个变量的值传递给另外一个变量通常有三种:赋值.浅拷贝.深拷贝 Python数据类型可氛围基本数据类型包括整型.字符串.布尔及None等,还有一种由基本数据类型作为最基本的元素所组成的像列表.元组.字典等. 在Python中基本数据类型的赋值.深浅拷贝没有任何意义,都是指向同一块内存地址,也不存在层次问题. 下面看基本数据类型的深浅拷贝 import copy n1 = 'abc' n2 = n1 n3 = copy.copy(n1) n4 = copy.deep

Python自动化 【第五篇】:Python基础-常用模块

目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re正则表达式 1.      模块介绍 1.1    定义 能够实现某个功能的代码集合(本质是py文件)  test.p的模块名是test包的定义:用来从逻辑上组织模块,本质就是一个目录(必须带有一个__init__.py文件) 1.2    导入方法 a) Import module b) Impo

Python开发【第五篇】:Python基础之杂货铺

字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator

python学习之路基础篇(第五篇)

前四天课程回顾 1.python简介 2.python基本数据类型 类: int:整型 | str:字符串 | list:列表 |tuple:元组 |dict:字典 | set:集合 对象: li = [11,22,33] #列表的一个对象 s = "MoHan" #字符串的一个对象 3.函数式编程 4.装饰器 @装饰器函数名 def func(): pass 其中@装饰器函数名,程序执行到此,会进行如下三步操作: 1.将func当做参数进行传递给装饰器函数并执行 2.将装饰器函数的返