【发布时间】:2017-09-15 00:33:48
【问题描述】:
我正在寻找有关如何最好地格式化元组输出的任何建议?它涉及一个大型数据集,我已经从包含 key1:value1 的两个字典中对 [key1: [value1, value2] 进行了规范化;键1:值2。目前,我的输出会产生类似以下示例的输出:{1: [771, 'MALICE MIZER'],... 但是,我正在尝试这种格式:MALICE MIZER(1) 771。如果有人有建议,我将不胜感激它。
sorted_aid2numplay= sorted(aid2numplay.items(), key=itemgetter(1), reverse=True)
#print(sorted_aid2numplay)
sorted_uid2numplay= sorted(uid2numplay.items(), key=itemgetter(1), reverse=True)
#print(sorted_uid2numplay)
sorted_aid2name = sorted(aid2name.items(), key=itemgetter(0), reverse=False)
#print(sorted_aid2name)
result = {}
for key in (aid2numplay.keys() | aid2name.keys()):
#if key in aid2numplay: result.setdefault(key, []).append(aid2numplay[key])
#if key in aid2name: result.setdefault(key, []).append(aid2name[key])
if key in aid2numplay and aid2name:
print((sorted_aid2name.itemgetter(0), key, sorted_uid2numplay.itemgetter(1))
#print(result)
【问题讨论】:
标签: python python-3.x sorting formatting tuples