python数组和 json字符串, list与numpy array互相转换

1 python数组和 json字符串 


json.dumps():接收python类型的数据作为参数,返回了一个str对象的encodedjson(从python数据转换为json);
json.loads():接收json字符串,返回python类型的数据(从json字符串转换为python数据)


例子:
JsonStr = json.dumps( Arr, ensure_ascii=False, encoding='UTF-8') 
Arr = json.loads(JsonStr)


2 python中list与numpy array 的转换

u = array([[1,2],[3,4]])

转换为list m = u.tolist()

移除m[0] m.remove(m[0])

转换为arra m = np.array(m)

3 round()

score :3.1415926535......
score = round(score, 4)
score :3.1415


Or不进行四舍五入,直接进行截断
1可以放大指定的倍数,然后取整,然后再除以指定的倍数。

#保留三位小数截断 python3
print(int(1.23456 * 1000) / 1000 )

Reference

https://blog.csdn.net/u011304615/article/details/70140901
https://blog.csdn.net/kl28978113/article/details/80196536

发布了1715 篇原创文章 · 获赞 380 · 访问量 247万+

猜你喜欢

转载自blog.csdn.net/tony2278/article/details/104859406