有两个对应的1对1关系列表。你知道吗
names = ["David", "Peter", "Kate", "Lucy", "Kit", "Jason", "Judy"]
scores = [1,1,0.8,0.2,0.4,0.1,0.6]
我想显示得分超过0.5的人,并在1行中显示:
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]
你可以在一行中完成,但如果你分阶段完成,阅读起来就容易多了。首先选择得分大于阈值的项目,然后对其排序。你知道吗
输出
以下是单行版本:
这应该可以做到:
输出:
如果需要排序的输出,还可以使用
OrderedDict
formcollections
模块。你知道吗印刷品:
David (1 points), Peter (1 points), Kate (0.8 points), Judy (0.6 points)
相关问题 更多 >
编程相关推荐