gpt4 book ai didi

python - 设置表的输出顺序

转载 作者:行者123 更新时间:2023-12-01 04:42:17 25 4
gpt4 key购买 nike

我正在尝试生成报告并将下表打印到​​ python 中的文件中:

from tabulate import tabulate
first_table_dict = {}
first_table_dict["Total agents"] = len(agentUserIdDict)
first_table_dict["Total users"] = len(pure_user_dict)
first_table_dict["Users with agent"] = len(pure_users_with_agents_dict)
first_table_dict["Users with invites"] = len(user_id_invite_dict)
first_table_dict["RUURL accounts"] = len(ruurl_set)
f.write(tabulate(first_table_dict.items(), headers = ["Category", "Total"]))

但是输出是:

Category              Total
------------------ -------
Users with invites 81533
Total users 90818
Users with agent 70060
Total agents 45571
RUURL accounts 81288

我知道字典没有排序。如何使列表按照我想要的顺序排列?

最佳答案

你的问题是你所期望的:

>>> {'a': 1, 'b': 2}.items()
[('a', 1), ('b', 2)]

事实上,有时你会得到

[('b', 2), ('a', 1)]

为什么不直接构造这个数据结构呢?

first_table = [
("Total agents" , len(agentUserIdDict)),
("Total users" , len(pure_user_dict)),
("Users with agent" , len(pure_users_with_agents_dict)),
("Users with invites" , len(user_id_invite_dict)),
("RUURL accounts" , len(ruurl_set))
]
f.write(tabulate(first_table, headers = ["Category", "Total"]))

关于python - 设置表的输出顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com