Python json序列化 反序列化,map,reduce,filter

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSU_GUO_LIANG/article/details/83176271
import json
# 序列化  反序列化
print(dir(json))
d1=dict(name='小米',age=2,score=99)
print(d1)
strs=json.dumps(d1)
print(strs)
d2=json.loads(strs)
print(d2)

print("-----------------------------------------")

sum2=lambda x1,x2:x1+x2
print(sum2(3,5))

from functools import reduce

l1=tuple([1,3,5,9]) # list,tuple 均可以
l2=reduce(lambda x1,x2:x1+x2,l1) 
# 二元操作符,先对集合第一个和第二个元素操作,得到的结果再与第三个数据运算,最后得到一个结果
print(l2)
l3=reduce(lambda x1,x2:x1+x2,l1,20)
print(l3)

l4=list(map(lambda x1:x1*2-1,l1))
print(type(l4))
print(l4)
ll=tuple([4,5,12,2,4])
new_list=list(map(lambda x1,x2:x1+x2,l1,ll))
print('new_list :',new_list)
# map应用于可迭代的项,接受2个参数,一个函数,一个序列。

l1=tuple(i for i in range(100) if i%8==0)
print(l1)
new1=list(filter(lambda x:x+1<40,l1))
print(new1)
#filter 过滤处理,使用一个自定义的函数过滤一个序列,并一次性返回过滤后的结果。

猜你喜欢

转载自blog.csdn.net/CSU_GUO_LIANG/article/details/83176271