var = 818
dict_1 = {var: var} # 创建字典时初始化
dict_2 = dict(var=var) # 创建字典时初始化,比上述方式更优雅
print(dict_1)
print(dict_2)
# 输出结果如下:
# {818: 818}
# {'var': 818}
_
作为名称使用,用作被丢弃的名称,是不会被用到的for _ in range(3):
print('hello')
直接json.dumps()
会报出如下错误:
typeerror: object of type 'ndarray' is not json serializable
可以写成:
arr = numpy.zeros(3)
json.dumps(arr, default=np.ndarray.tolist)
# ``default(obj)`` is a function that should return a serializable version
# of obj or raise TypeError. The default simply raises TypeError.