首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何漂亮地打印字典列表

如何漂亮地打印字典列表
EN

Stack Overflow用户
提问于 2020-07-03 03:28:40
回答 2查看 403关注 0票数 0

我有以下列表:

代码语言:javascript
复制
import json
import pprint

scoring_dictionary=[{'IMEI': '867',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '430',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '658',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '157',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '521',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12}]

我想在单元格的标准输出中漂亮地打印出来。在一个网上类似的问题上看过这个代码,

代码语言:javascript
复制
print("The list of dictionaries per scored asset id: {0}".format(json.dumps(json.loads(scoring_dictionary), indent=4)))

但产出仍然很差。

代码语言:javascript
复制
"The list of dictionaries per scored asset id: [{'IMEI': '867', 'Timestamp': '2020-07-02 13-29-24', 'RAG': 'G', 'Probability': 0.12}, {'IMEI': '430', 'Timestamp': '2020-07-02 13-29-24', 'RAG': 'G', 'Probability': 0.12}...]" # a straight text of string without intends

除了字典列表之外,我还想打印一本字典,看起来像这样:

代码语言:javascript
复制
{'failed_devices': ['334', '897', '485'], 'partially_succeeded_devices': ['867', '430', '658', '157', '521'], 'total_failures': 705, 'total_partially_succeeded': 268, 'total_succeeded': 26, 'total': 999, 'timestamp': '2020-07-02 13-29-24', 'failures_threshold': 0.1, 'failed_percentage_out_of_total': 0.7057057057057057}

我想以与上面的字典列表完全相同的方式打印出来。

更新这个比pprint更有效。感谢所有的答案。感谢您的关心。

代码语言:javascript
复制
print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(scoring_dictionary, indent=2)))

提前感谢您的帮助。

EN

回答 2

Stack Overflow用户

发布于 2020-07-03 03:38:13

想要更漂亮的照片,可以查看pprint

代码语言:javascript
复制
import pprint 
pprint.pprint(data)


pprint.pprint(data)

[{'IMEI': '867',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '430',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '658',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '157',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '521',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'}]
票数 1
EN

Stack Overflow用户

发布于 2020-07-03 04:05:55

Similar question

代码语言:javascript
复制
#list of dictionaries
scoring_dictionary=[{'IMEI': '358639059721867',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721430',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721658',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721157',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721521',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721713',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12}]

#dictionary
devices_succeed_failed={'failed_devices': ['334', '897', '485'], 'partially_succeeded_devices': ['867', '430', '658', '157', '521'], 'total_failures': 705, 'total_partially_succeeded': 268, 'total_succeeded': 26, 'total': 999, 'timestamp': '2020-07-02 13-29-24', 'failures_threshold': 0.1, 'failed_percentage_out_of_total': 0.7057057057057057}
代码语言:javascript
复制
# WORKED FOR BOTH CASES
print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(scoring_dictionary, indent=2)))

print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(devices_succeed_failed, indent=2)))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62703729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档