python中内置函数和json、pickle数据序列化

来源:互联网 发布:c语言编写小游戏 编辑:程序博客网 时间:2024/06/08 15:42

内置函数

map

res = map(lambda n:n**n, range(10))res1 = [lambda i:i*2 for i in range(10)]for i in res1:    print(i)

reduce

import functoolsres = functools.reduce(lambda  x, y:x+y, range(10))print(res)

对字典排序的函数

#对字典排序a = {6:2, 3:12, 99:11, -1:2}print(sorted(a.items()))#指定排序方式print(sorted(a.items(), key = lambda x:x[1]))

将两个数组拼成一个数组

a = [1,2,3,4]b = ['a', 'b', 'c', 'd']for i in zip(a,b):    print(i)

jsonpickle数据序列化

# info = {#     'name':'dancheng',#     'age':20# }# f = open('test.text', 'w')# f.write(str(info))                  #将json对象变成字符串# f.close()# f = open('test.text','r')# data = eval(f.read())             #将字符串转成json对象# f.close()# print(data['name'])

调用json

info = {    'name':'dancheng1',    'age':20,    'func':sayhi('dancheng')}f = open('test.text', 'w')f.write(json.dumps({1:2}))f.close()

调用pickle

import pickleinfo = {    'name':'dancheng1',    'age':20,}f = open('test.text', 'wb')f.write(pickle.dumps(info))f.close()import picklef = open('test.text', 'rb')data = pickle.loads(f.read())f.close()print(data)