这是我的代码示例:
budi = {"Name" : "Budi", "Gender" : "Male", "Age" : 18}
ahmad = {"Name" : "Ahmad", "Gender" : "Male", "Age" : 7}
ika = {"Name" : "Ika", "Gender" : "Female", "Age" : 18}
marged = [budi, ahmad, ika]
我想要这样的结果,例如基于18岁:
The oldest participants are: Budi and Ika
答案 0 :(得分:-1)
根据您在问题中提出的问题,这是代码。请完成这个非常简单的实现。
from operator import itemgetter
budi = {"Name" : "Budi", "Gender" : "Male", "Age" : 1}
ahmad = {"Name" : "Ahmad", "Gender" : "Male", "Age" : 7}
ika = {"Name" : "Ika", "Gender" : "Female", "Age" : 18}
marged = [budi, ahmad, ika]
newlist = sorted(marged, key=itemgetter('Age'), reverse=True)
maxAge = newlist[0]["Age"]
finalList = [newlist[0]["Name"]]
for person in newlist[1:]:
if person["Age"] == maxAge:
finalList.append(person["Name"])
if len(finalList) == 1:
print "The oldest participant is: " + finalList[0]
else:
print "The oldest participants are: ",
for name in finalList:
print name+" ",