how to dynamically choose row labels using tabulate in python

[亡魂溺海] 提交于 2021-02-11 00:59:44

问题


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

When I run the following:

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

I get the following:

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]

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

I've got:

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?


回答1:


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())))


来源:https://stackoverflow.com/questions/30609716/how-to-dynamically-choose-row-labels-using-tabulate-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!