1.两个列表[1,5,7,9]和[2,2,6,8]合并为1,22,5,6,7,8,9]
list1=[1,5,7,9]
list2=[2,2,6,8]
list1.extend(list2)
print(list1)
list1.sort(reverse=False)
print(list1)
输出结果:
2.用datetime模块打印当前时间
import datetime
a=str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print(a)
输出结果:
3.自定义异常的代码
def fn():
try:
for i in range(7):
if i>2:
raise Exception("数字大于2了")
except Exception as e:
print(e)
fn()
输出结果:
4.正则表达式匹配中, (.*)和 (.*?) 匹配区别?
(.*)是贪婪匹配,会把满足正则的尽可能多的往后匹配
(*?)是非贪婪匹配,会把满足正则的尽可能少匹配
import re
s = "哈哈嘻嘻"
res1 = re.findall("(.*)",s)
print("贪婪匹配",res1)
res2 = re.findall("(.*?)",s)
print("非贪婪匹配",res2)
输出结果:
5.[[1,2],[3,4],[5,6]]一行代码展开该列表得出[1,2,3,4,5,6]
a=[[1,2],[3,4],[5,6]]
x=[j for i in a for j in i]
print(x)
import numpy as np
a=[[1,2],[3,4],[5,6]]
b = np.array(a).flatten().tolist()
print(b)
6.x="abc",y="def",z=["d","e","f"],分别求出xjoin(y)和xjoim(z)返回的结果
x="abc"
y="def"
z=["d","e","f"]
m=x.join(y)
n=x.join(z)
print(m)
print(n)
输出结果: