按值排序并正确打印密钥和值。蟒蛇

时间:2016-10-02 06:34:12

标签: python sorting dictionary

问题1 :如何通过询问用户,I.E。添加到字典中?输入(“写入运行者名称”),输入(“写入已用时间!”)

问题2 : 我想像这样输出。 在我的代码下面......草稿:)

import operator

词典

runners = {"John": 9, "Mike": 2, "Venera": 4}

输出:

1st. "Entered runner name ", came at, "input time"
2nd. "Entered runner name ", came at, "input time"
3rd. "Entered runner name ", came at, "input time"

下面从最少到更多

进行排序
s = sorted(runners.items(), key=operator.itemgetter(1))
print(s)

示例将让您充分了解我的问题。所以,示例代码就是这样,我只是想让它变得更容易。

names =[]
array_times = []

循环问题,不写一堆输入 对于范围(3)中的i:

 name=input("Please write the name of Runner!")
    #Appending name to the array name above!
    names.append(name)
    time_elapsed = float(input("Please write the time you spend!"))
    #Appending it to the array above!
    array_times.append(time_elapsed)
# Just to make it easier to understand, assigning the values in arrays above to variables. 
runner1= names[0]
runner2=names[1]
runner3=names[2]
time1=array_times[0]
time2=array_times[1]
time3=array_times[2]

编写if-else语句来检查哪一个是哪个地方。

if time1<time2 and time1<time3 and time2<time3:#First winner, second in second, third is last.
    print (runner1,  "wins! His time is", time1, runner2, "is second place. His time is", time2, "and ", runner3, "is third place. his time is ", time3)
elif time2<time1 and time2<time3 and time1<time3:#Second winner, first in second place, third is last.
    print (runner2,  "wins! His time is", time2, runner1, "is second place. His time is", time1, "and ", runner3, "is third place. his time is ", time3)
elif time3<time1 and time3<time2 and time1<time2:#Third winner, First is second, second is last.
    print (runner3,  "wins! His time is", time3, runner1, "is second place. His timae is", time1, "and ", runner2, "is third place. his time is ", time2)
elif time3<time1 and time3<time2 and time2<time1:#Third winner, second is second, first is last.
     print (runner3,  "wins! His time is", time3, runner2, "is second place. His time is", time2, "and ", runner1, "is third place. his time is ", time1)
elif time2<time1 and time2<time3 and time3<time1:#Third winner, second is second, first is last.
     print (runner2,  "wins! His time is", time2, runner3, "is second place. His time is", time

3 个答案:

答案 0 :(得分:0)

如python doc

中所述
  

最好将字典视为一组无序的键:值对,并要求键是唯一的(在一个字典中)

使用OrderedDict

from collections import OrderedDict

no_of_runners = int(input())

runners = dict()
for i in range(no_of_runners):
    name = input()
    time = float(input())
    runners[name] = time
sorted_runners = OrderedDict(sorted(runners.items(), key=lambda t: t[1]))

runners_list = [(i,j) for i,j in sorted_runners.items()]

print (runners_list[0][0],  "wins! His time is", runners_list[0][1], runners_list[1][0], "is in second place. His time is", runners_list[1][1], "and ", runners_list[2][0], "is in third place. his time is ", runners_list[2][1])

答案 1 :(得分:0)

python3

import operator

runners = {}

for x in range(3):
    name = input("name: ")
    elapsed_time = int(input("time: "))
    runners[name] = elapsed_time

templates = [
    "{name} wins! His time is {time}.",
    "{name} is in second place. His time is {time}",
    "and {name} is in third place. His time is {time}"
]

for idx, (name, elapsed_time) in enumerate(sorted(runners.items(), key=operator.itemgetter(1))):
    print(templates[idx].format(name=name, time=elapsed_time), end=" ")
print()

答案 2 :(得分:0)

Python 2.7

runners={}
for i in range(3):
    name=raw_input("Please write the name of Runner!")
    time_elapsed = float(raw_input("Please write the time you spend!"))
    runners[name]=time_elapsed
position=[]
for name, elapsed in sorted(runners.iteritems(), key=lambda elapsed: elapsed[1]):
    position.append((name, elapsed))
print position[0][0],  "wins! His time is", position[0][1],'\n', position[1][0], "is second. His time is", position[1][1], "\n", position[2][0], "is third. His time is ", position[2][1]

Python3

runners={}
for i in range(3):
    name=input("Please write the name of Runner!")
    time_elapsed = float(input("Please write the time you spend!"))
    runners[name]=time_elapsed
position=[]
for name, elapsed in sorted(runners.items(), key=lambda elapsed: elapsed[1]):
    position.append((name, elapsed))
print ('\n',position[0][0],  "wins! His time is", position[0][1],'\n', position[1][0], "is second. His time is", position[1][1], "\n", position[2][0], "is third. His time is ", position[2][1])