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

问题描述:

我在寻找关于如何最好地格式化元组输出的任何建议?它涉及一个大数据集,我已经从包含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 
+0

谢谢!工作完美。 – pythonuser890