2017-10-11 74 views
1

有兩個對應的1對1關係列表。Python通過值(一個列表)排序兩個通訊錄列表

names = ["David", "Peter", "Kate", "Lucy", "Kit", "Jason", "Judy"] 
scores = [1,1,0.8,0.2,0.4,0.1,0.6] 

我想告訴誰在1線場得分超過0.5,顯示:

Peter (1 point), David (1 point), Kate (0.8 point), Judy (0.6 point) 

我想的是:

import operator 

names = ["David", "Peter", "Kate", "Lucy", "Kit", "Jason", "Judy"] 
scores = [1,1,0.8,0.2,0.4,0.1,0.6] 

dictionary = dict(zip(names, scores)) 

dict_sorted = sorted(dictionary.items(), key=operator.itemgetter(1), reverse=True) 

print dict_sorted 

它提供:

[('Peter', 1), ('David', 1), ('Kate', 0.8), ('Judy', 0.6), ('Kit', 0.4), ('Lucy', 0.2), ('Jason', 0.1)] 

它如何才能進一步得到結果想?注意:需要從大到小的排序結果。

2再爲測試目的列表:

names = ["Olivia","Charlotte","Khaleesi","Cora","Isla","Isabella","Aurora","Amelia","Amara","Penelope","Audrey","Rose","Imogen","Alice","Evelyn","Ava","Irma","Ophelia","Violet"] 
scores = [1.0, 1.0, 0.8, 0.2, 0.2, 0.4, 0.2, 0.0, 1.0, 0.2, 0.4, 0.2, 1.0, 0.0, 0.8, 0.0, 1.0, 0.0, 0.6] 
+1

你不需要那個詞典。只需操作'zip'返回的元組列表,如mentalita的答案所示。順便說一句,在Python 3中,'zip'不返回一個列表,它返回一個迭代器,但mentalita的代碼在兩個版本中都可以正常工作。 –

+0

@ PM2Ring,謝謝。 –

回答

6

這應該做的伎倆:

names = ["David", "Peter", "Kate", "Lucy", "Kit", "Jason", "Judy", "Mark", "John", "Irene"] 
scores = [1,1,0.8,0.2,0.4,0.1,0.6,0.7,0.3,1.2] 

print(', '.join('{} ({} points)'.format(name, points) for name, points in sorted(zip(names, scores), key=__import__('operator').itemgetter(1), reverse=True) if points > 0.5)) 

輸出:

Irene (1.2 points), David (1 points), Peter (1 points), Kate (0.8 points), Mark (0.7 points), Judy (0.6 points) 
+0

哇! 1班輪魔術師! –

+0

當我將比分改爲更長的列表時,結果產生的結果沒有排序(從大到小)。你能看看嗎? –

+0

@MarkK儘管您的問題中的代碼執行排序,但您實際上沒有提到您希望輸出進行排序。你應該解決這個問題。 –

1

你也使用OrderedDict形式collections模塊,如果你想要排序的輸出。

from collections import OrderedDict 

names = ["David", "Peter", "Kate", "Lucy", "Kit", "Jason", "Judy"] 
scores = [1,1,0.8,0.2,0.4,0.1,0.6] 

dict_sorted = OrderedDict((k, v) for k, v in zip(names, scores) if v > 0.5) 
print(', '.join('{} ({} points)'.format(k, v) for k, v in dict_sorted.items())) 

打印: David (1 points), Peter (1 points), Kate (0.8 points), Judy (0.6 points)

+0

只是在「dict_sorted」行末尾「)」,「a.items」應該是「dict_sorted .items」。 @Alex Pshenko,你能否告訴我們結果可以從大到小排序?謝謝。 –

2

爲此,您可以在同一行,但它容易得多,如果你分階段做閱讀。首先選擇分數大於閾值的項目,然後對它們進行排序。

import operator 

names = ["Olivia","Charlotte","Khaleesi","Cora","Isla","Isabella","Aurora","Amelia","Amara","Penelope","Audrey","Rose","Imogen","Alice","Evelyn","Ava","Irma","Ophelia","Violet"] 
scores = [1.0, 1.0, 0.8, 0.2, 0.2, 0.4, 0.2, 0.0, 1.0, 0.2, 0.4, 0.2, 1.0, 0.0, 0.8, 0.0, 1.0, 0.0, 0.6] 

threshold = 0.5 
lst = [(name, score) for name, score in zip(names, scores) if score > threshold] 
lst.sort(reverse=True, key=operator.itemgetter(1)) 
print(lst) 

輸出

[('Olivia', 1.0), ('Charlotte', 1.0), ('Amara', 1.0), ('Imogen', 1.0), ('Irma', 1.0), ('Khaleesi', 0.8), ('Evelyn', 0.8), ('Violet', 0.6)] 

下面是一行代碼版本:

print(sorted(((name, score) for name, score in zip(names, scores) if score > 0.5), reverse=True, key=operator.itemgetter(1))) 
+0

非常感謝你的步驟! –

+0

我可以請求你的諒解嗎? mentalita的回答是第一個,他也提供了一個更新。你很有名,而且有很多分數。你能不能介意我選擇mentalita作爲這個問題的最佳答案? –

+0

你的幫助和分享非常重要。請不要生氣。 –

相關問題