不知不觉,学习Python这门编程语言好像快1年半(还是很菜),之前学了忘,忘了学,感觉自己总结、笔记做的少;最近工作要写些脚本,就总结下自己学到、用到的部分知识;
最开始接触Python,要拼接字符串用的是 连接符号(+);后面再学习的时候,知道join()方法来更高效,所以就换了;
def test_1214(self):
str_abc = '2019-12-14'
str_b = '2019' + '-' + '12' + '-' + '14'
str_c = ''.join(['2019', '-', '12', '-', '14'])
str_d = '-'.join(('2019', '12', '14'))
print(str_abc)
print(str_b)
print(str_c)
print(str_d)
上面的四种str都是一种格式,‘yyyy-MM-dd’
我工作中,有些时间是’yyyyMMdd’,但是传参的用的是’yyyy-MM-dd’;
用到join()拼接,可以这样来处理:
def change_str(self, test_day):
"""
:param test_day: yyyyMMdd
:return: yyyy-MM-dd
"""
return '-'.join((test_day[0:4], test_day[4:6], test_day[-2:])
join() 作用的是 序列 【上面只是用的 tuple】
lambda表达式语法精简,基本语法是 在冒号(:)左边放参数,【多个参数使用’,'隔开】右边是返回值;
针对刚才所写的方法change_str() ,用lambda表达式,可以这样处理:
def test_12142(self):
old = self.change_str('20191214')
print(old)
f = lambda x, y, z: '-'.join([x, y, z])
new = f('2019', '12', '16')
print(new, type(new))
map() 会根据提供的函数对指定序列做映射 (在python3中返回迭代器)
假设我手上有个time的list,我要对其每个元素都做加 ‘-’ 的处理,
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
用map(),可以这样处理:
def test_12143b(self):
# 改变time_list的每个元素
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(map(self.change_str, time_list))
print(new_list)
常用匿名函数,就如下:
def test_12143a(self):
# 改变time_list的每个元素
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(map(lambda x: self.change_str(x), time_list))
print(new_list)
a = x if 条件 else y 这就是三元操作符的语法;
是说 当条件成立、为True的时候,a的赋值为x;不然就赋值为y
def if_else(self, x, y):
# abc的赋值
if x < y:
abc = x
else:
abc = y
# 条件表达式 简单写
ABC = x if x < y else y
print(abc, ABC)
filter() 函数用于过滤序列,过滤掉不符合条件的元素。(Python3 返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换)
还是那个time的list,我想找出来所有 dd='02’的,
用到filter(),可以这样处理:
def test_12144b(self):
# 筛选出 time_list中 每个结尾为'02'的元素(组成新list)
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(filter(lambda x: x[-2:] == '02', time_list))
print('过滤下', new_list)
# 对其处理格式,加上 '-'
last_list = list(map(lambda x: '-'.join((x[0:4], x[4:6], x[-2:])), new_list))
print('加上 - ', last_list)
如果是不使用匿名函数,要使用条件表达式的话,如下处理:
def element_end02(self, e):
return e if e[-2:] == '02' else False
def test_12144a(self):
# 筛选出 time_list中 每个结尾为'02'的元素(组成新list)
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(filter(self.element_end02, time_list))
print('过滤下', new_list)
# 对其处理格式,加上 '-'
last_list = list(map(self.change_str, new_list))
print('加上 - ', last_list)
列表生成式 是Python提供的一种生成列表的简洁形式,就可以相对简单,
工作中,在创建list的时候,会常用到:
def test_c1(self):
iterable = list((1, 2, 3, 4))
L = []
for iter_var in iterable:
L.append(iter_var + 2)
print(L)
用上列表生成式,就可以这样处理:
def test_c2(self):
iterable = list((1, 2, 3, 4))
L_new = [i + 2 for i in iterable]
print(L_new)
还是拿前面的time的list来说,我想找到每个元素的年份(成一个新list),用列表生成式:
def test_12145a1(self):
# 找出time_list的每个元素的年份
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = [x[0:4] for x in time_list]
print(new_list)
用map() + 匿名函数:
def test_12145a2(self):
# 找出time_list的每个元素的年份
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(map(lambda x: x[0:4], time_list))
print(new_list)
再深入些,我想找的是 dd='01’元素的年份,用列表生成式【 for 循环后面加上 if 判断】:
def test_12145b1(self):
# 找出time_list的每个结尾为01元素的 月+天
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = [x[4:] for x in time_list if x[-2:] == '01']
print(new_list)
使用map() 和 filter()
def test_12145b2(self):
# 找出time_list的每个结尾为01元素的 月+天
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list(map(lambda x: x[4:] if x[-2:] == '01' else None, time_list))
print(list(filter(None, new_list)))
在列表生成式中,也可以用多层 for 循环来生成列表;
def test_12146a1(self):
# 全排列
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = ['_'.join([x, y]) for x in time_list for y in time_list]
return new_list
实际就等同:
def test_12146a2(self):
# 全排列
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new_list = list()
for x in time_list:
for y in time_list:
new_list.append('_'.join([x, y]))
return new_list
再问下,这样的表达式 是不是也可以增加if条件:
答案是:Yes
def test_12146b1(self):
# 全排列 + if 条件
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new = list()
for x in time_list:
for y in time_list:
if x[-2:] == '01' and y[-2:] == '02':
new.append('_'.join([x, y]))
print(new)
def test_12146b2(self):
# 全排列 + if 条件
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new = list()
for x in time_list:
if x[-2:] == '01':
for y in time_list:
if y[-2:] == '02':
new.append('_'.join([x, y]))
print(new)
def test_12146b3(self):
# 全排列 + if 条件
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new = ['_'.join([x, y]) for x in time_list for y in time_list if x[-2:] == '01' and y[-2:] == '02']
print(new)
def test_12146b4(self):
# 全排列 + if 条件
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
new = list()
for x in time_list:
for y in time_list:
if y[-2:] == '02':
if x[-2:] == '01':
new.append('_'.join([x, y]))
print(new)
说这么多,用个真实情景来加深理解:
还是拿time的list来说,我在实际工作时,有时候需要传参-开始时间、结束时间(前提当然是开始时间<=结束时间),我基本是2个for循环,再加个if条件,如果用到多层表达式,可以这样处理:
def test_12146c(self):
time_list = ['20191201', '20191202', '20190101', '20190102', '20190201', '20190202', '20190301', '20190302']
# for a in time_list:
# for b in time_list:
# if a <= b:
# self.examination(account_no, bank, a, b, country)
fact = [(x, y) for x in time_list for y in time_list if x <= y]
for f in fact:
print('开始时间{}、结束时间{}'.format(f[0], f[1]))
# self.examination(account_no, bank, f[0], f[1], country)
代码中注释的是我真实用的;
用到多层表达式,就把(开始时间、结束时间)重新组成一个list,再对其做个for循环;
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie