社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何列出参加python比赛的参赛者的姓名、时间和位置?

Alando123 • 5 年前 • 349 次点击  

基本上,我需要为4个组创建一个参与者列表(我想对这些组进行排序),记录他们在比赛中的时间和排名,并生成一个格式整洁的结果表。

racers = [(i, l[i]) for i in l]

places = sorted(racers, key = lambda i: int(i[1]))

print('1st Place: {}'.format(places[0][0]))
print('2nd Place: {}'.format(places[1][0]))
print('3rd Place: {}'.format(places[2][0]))

这是我当前的代码:

l = []
a = int(input("Enter the amount of racers for Anstey: "))
g = int(input("Enter the amount of racers for Gordon: "))
h = int(input("Enter the amount of racers for Helps: "))
r = int(input("Enter the amount of racers for Rawle: "))
total_racers = a+g+h+r

print("Total Racers:",total_racers)

for i in range(total_racers):
  racer_name = (input("Participant Name: "))
  racer_time = int(input("Participant Time: "))
  time_name = racer_time,racer_name
  l.append(time_name)

print('\n',*l, sep='\n')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53152
 
349 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Alando123
Reply   •   1 楼
Alando123    5 年前

在这里。

import xlsxwriter

# XLSXWRITER uses O as first row or column
row = 1 # 2nd row... leave room for Headers
col = 0 # 1st column

# Assign Names and Registration Numbers to Contestants
while True:
    print("\nEnter q to stop entering names", "\n")
    while True:
        fname = input("Enter First Name: ")
        if fname.isalpha():
            break
        else:
            print("Please ONLY use letters A-Z")
            continue
    if str(fname) == 'q':
            break

    while True:
        lname = input("Enter Last Name: ")
        if lname.isalpha():
            break
        else:
            print("Please ONLY use letters A-Z")
            continue

    while True:
        house = input("Enter House: ")
        if house.isalpha():
            break
        else:
            print("Please ONLY use letters A-Z")
            continue


    dob = input("Enter Date of Birth 'dd/mm/yyyy': ")

    while True:
        try:
            regno = int(input("Enter Registration Number: "))
        except ValueError:
            print(" That is not a valid number")
            continue
        else:
            break

    print("\n")
    date = workbook.add_format({'num_format': 'dd/mm/yyyy'})
    worksheet.write(row, col, fname) # First Name
    worksheet.write(row, col+1, lname) # Last Name
    worksheet.write(row, col+2, house) # House
    worksheet.write(row, col+3, dob) # Date of Birth
    worksheet.write(row, col+4, regno, date) # Reg. No. # 28/02/13
    row = row+1
henrywongkk
Reply   •   2 楼
henrywongkk    5 年前

l 已经是一个列表,不需要创建另一个列表 racers 用于排序。如果需要,可以指定 racers = l

time_name (racer_time, racer_name) ,和 key 排序应该是 i[0] ,这是 racer_time ,您现在正在使用 i[1] racer_name )排序

racers = l
sorted(l, key = lambda i: i[0])

或者你应该换个位置 赛车手姓名 ,例如。 time_name = racer_name,racer_time ,因为我想你会想打印 最后,但是 places[0][0] 实际上是 比赛时间