Python推导式应用
程序员文章站
2024-01-25 19:30:16
# -*- coding: utf-8 -*-# @Author: 二师父# @Time: 2020-08-27# @Blog:# @Describe: Python推导式import re案例 = Trueif 案例: # 1.列表推导式 l = [x for x in 'Python'] print(l) # 2.字典推导式 d = {y:x for x, y in enumerate('Python')} print(d......
# -*- coding: utf-8 -*-
# @Author: 二师父
# @Time: 2020-08-27
# @Blog: https://blog.csdn.net/weixin_39181440/article/details/108262759
# @Describe: Python推导式
import re
案例 = True
if 案例:
# 1.列表推导式
l = [x for x in 'Python']
print(l)
# 2.字典推导式
d = {y:x for x, y in enumerate('Python')}
print(d)
# 3.集合推导式
s = {x for x in 'Python'}
print(s)
应用 = True
if 应用:
# 这是一个非标准Python数据结构
str = '''
{PrdCode:"T80264",PrdName:"招商银行挂钩黄金看涨三层区间三个月结构性存款(代码:T80264)",PrdBrief:"挂钩黄金看涨三层区间三个月结构性存款",TypeCode:"010011",AreaCode:"",Currency:"人民币",BeginDate:"2020-08-28",EndDate:"2020-09-03",ShowExpireDate:"2020-12-03",ExpireDate:"2020-12-03",Status:"A",NetValue:"",IsNewFlag:"False",Term:"91天-180天(含)",Style:"浮动收益型",InitMoney:"0",IncresingMoney:"0",Risk:"R1(保守型)",FinDate:"91",SaleChannel:"1|2|3|4",SaleChannelName:"网上|手机|PAD|网点",IsCanBuy:"false",REGCode:"",CapitalProtectName:"",RateFlag:"0",CRateType:"",RateLow:"",RateHigh:"",IsInfinite:"0",ShowExpectedReturn:"",ProxyText:"",IsSA:"0",saaCod:"",funCod:"",datCyl:"0",invUntName:"天",Id:"44354"}
'''
def turn_dict(data):
return {x[0]:re.sub(r'^"|"$', '', x[1]) for x in [re.search(r'(.*?):(.+)', i).groups() for i in re.sub(r'^\s*{|}\s*$', '', data).split(',')]}
data = turn_dict(str)
print(data)
print(data['PrdCode'])
本文地址:https://blog.csdn.net/weixin_39181440/article/details/108262759