2017-04-19 102 views
0

我在尋找關於如何最好地格式化元組輸出的任何建議?它涉及一個大數據集,我已經從包含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) 

回答

0

你可以使用一個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