使用同一元组的不同元素格式化输出



我正在寻找有关如何从元组中最佳格式输出的任何建议?它涉及一个大数据集,该数据集我已经从包含key1:value1的两个字典中对[key1:[value1,value2]进行了归一化;key1:value2。目前,我的输出产生了一个示例的输出:{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)

您可以使用for循环和format方法来打印字典中的结果。请参阅下面的示例

mydict = {1: [771, 'MALICE MIZER'], 43: [536, 'HARRY POTTER']}
for key,value in mydict.items():
    print '{v[1]}({k}) {v[0]}'.format(k=key,v=value)

导致

MALICE MIZER(1) 771
HARRY POTTER(43) 536

最新更新