如何在python中使用表格动态选择行标签 [英] how to dynamically choose row labels using tabulate in python

查看:49
本文介绍了如何在python中使用表格动态选择行标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,其中键是字符串,值是整数列表.它的创建如下:

I have a dictionary where the keys are strings and the values are lists of integers. It is created as follows:

table_dict_of_lists = {}
for label in return_dict_keys:
    temp_list = []
    for dict_list in stats_list_dict_list:
        temp_list.append(len(dict_list[label]))
    table_dict_of_lists[label] = temp_list

当我运行以下命令时:

for k, v in table_dict_of_lists.iteritems():
    print k, v

我得到以下信息:

agentsGtX [566, 0, 0, 69, 134]
pure_user_dict [11818, 0, 0, 627, 1910]
inv_a_id_user_id [857, 0, 0, 73, 135]
user_email_id_dict [18005, 0, 0, 800, 2669]
ruurl_set [1288, 0, 0, 107, 247]
user_id_invite_dict [9772, 0, 0, 473, 1578]
pure_users_with_agents_dict [11060, 0, 0, 580, 1825]
user_id_email_dict [18066, 0, 0, 800, 2682]

这一切都是为了尝试使用 tabulate 将此数据动态打印到表格中.

This was all in the service of trying to dynamically print this data to table using tabulate.

我有:

first_table = table_dict_of_lists
f.write(tabulate(first_table, headers = "keys"))

不用说,这将键作为列标题.我看过这里,但我没有看到答案.

Needless to say, this puts the the keys as the column headers. I've looked here but I don't see the answer.

如何获取键作为标签行?

How to I get the keys as label rows?

推荐答案

文档似乎从未暗示库中支持行标题.这是一种可能的解决方法,至少可以使其正确制表:

The documentation never seems to suggest that row headers are supported in the library. Here's a possible workaround to at least get it to tabulate correctly:

f.write(tabulate(([k] + v for k, v in first_table.iteritems())))

这篇关于如何在python中使用表格动态选择行标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆