Python语法知识点三

标签: Python 分类: Python 创建时间:2020-02-02 12:56:21 更新时间:2023-10-20 11:23:26

1.多线程ping Ip地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## 多线程ping
## 订阅地址
configs = []
## 全部地址
allNode=[]
## 网络延迟
minRtt=400
## ip队列
ipqueue=queue.Queue()
def queue_ping():
while not ipqueue.empty():
global configs
## 全部地址
global allNode
## 网络延迟
global minRtt
nodeStr = ipqueue.get()
ip=nodeStr['add']
## 临时user
tempNode={}
tempNode['address']=ip
tempNode['port']=nodeStr['port']
users=[]
usersT={}
usersT['alterId']=int(nodeStr['aid'])
usersT['id']=nodeStr['id']
users.append(usersT)
tempNode['users']=users

rtt=pingIp(ip)
nodeStr['rtt']=rtt
if rtt !=-1 :
allNode.append(nodeStr)
## 测试最快节点
if rtt <= minRtt :
## 清空数组
configs.clear()
## 加入json数组
configs.append(tempNode)
minRtt=rtt

## 开启多线程
def ping_threading():
threads = []
WORD_THREAD=50
for i in range(WORD_THREAD):
thread = threading.Thread(target=queue_ping)
thread.start()
threads.append(thread)

for thread in threads:
thread.join()

2.Python3 字典dict判断是否包含键值–in 操作符

python3不支持 dick.has_key() 函数

1
2
3
dict={"name":"alice","age":7}
if 'age' in dict:
print('键age存在')

3.正则表达式

1
2
mapobj=re.match(r'(?:trojan|ssr)(?<=trojan|ssr).*?(?=(trojan|ssr|$))',links,re.M|re.I)
print(mapobj)

执行上面的代码时,出现了:look-behind requires fixed-width pattern 错误。

测试工具给出的代码为:

1
2
3
4
import re
pattern = re.compile(ur'(?:trojan|ssr)(?<=trojan|ssr).*?(?=(trojan|ssr|$))')
str = u''
print(pattern.search(str))

这个无法完成,python的re模块并不支持变长的后发断言,只支持定长的后发断言。也就是说(?<=trojan|ssr)不能跟一个不确定的长度内容。

1
2
3
4
5
6
7
8
## 为了获取其中的hello world
<html>hello world</html>

## 刚开始的正则
(?<=<([a-zA-Z]+>)).*(?=</\1>)

## 后来的正则
<([a-zA-Z]+)>(.*)</\1>

4.python查找字符串

Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

我记得在某一种语言中,会有indexOf方法,可以支持或操作符,比如这样写:

1
str.indexOf('a|b')

通过这种方法,就是找到下一个a或者是b的位置。我又查询了javascript和java的indexOf用法,发现了,没有这个方法。难道是我记错了?

5.判断对象的类型

1
2
3
4
5
6
7
8
9
## type可以输出一个对象的类型
te=b'dsd'
type(te)
## 输出:<class 'bytes'>
print(type(te))

isinstance(te,bytes)
## 输出 True
print(isinstance(te,bytes))

type主要用于获取未知变量的类型
isinstance主要用于判断A类是否继承于B类

小额赞助
本人提供免费与付费咨询服务,感谢您的支持!赞助请发邮件通知,方便公布您的善意!
**光 3.01 元
Sun 3.00 元
bibichuan 3.00 元
微信公众号
广告位
诚心邀请广大金主爸爸洽谈合作
每日一省
isNaN 和 Number.isNaN 函数的区别?

1.函数 isNaN 接收参数后,会尝试将这个参数转换为数值,任何不能被转换为数值的的值都会返回 true,因此非数字值传入也会返回 true ,会影响 NaN 的判断。

2.函数 Number.isNaN 会首先判断传入参数是否为数字,如果是数字再继续判断是否为 NaN ,不会进行数据类型的转换,这种方法对于 NaN 的判断更为准确。

每日二省
为什么0.1+0.2 ! == 0.3,如何让其相等?

一个直接的解决方法就是设置一个误差范围,通常称为“机器精度”。对JavaScript来说,这个值通常为2-52,在ES6中,提供了Number.EPSILON属性,而它的值就是2-52,只要判断0.1+0.2-0.3是否小于Number.EPSILON,如果小于,就可以判断为0.1+0.2 ===0.3。

每日三省
== 操作符的强制类型转换规则?

1.首先会判断两者类型是否**相同,**相同的话就比较两者的大小。

2.类型不相同的话,就会进行类型转换。

3.会先判断是否在对比 null 和 undefined,是的话就会返回 true。

4.判断两者类型是否为 string 和 number,是的话就会将字符串转换为 number。

5.判断其中一方是否为 boolean,是的话就会把 boolean 转为 number 再进行判断。

6.判断其中一方是否为 object 且另一方为 string、number 或者 symbol,是的话就会把 object 转为原始类型再进行判断。

每日英语
Happiness is time precipitation, smile is the lonely sad.
幸福是年华的沉淀,微笑是寂寞的悲伤。