我无法理解这段代码
I'm having trouble understanding this code
我是 python 的新手,正在尝试通过在 hackerrank 上练习来学习。我不理解这个列表概念。这就是问题
输入格式:
第一行包含一个整数, ,学生人数。
随后的行描述了每个学生的行;第一行包含学生姓名,第二行包含他们的成绩。
约束条件
总会有一个或多个学生的成绩倒数第二。
输出格式:
写出物理成绩倒数第二的所有学生的姓名;如果有多个学生,请按字母顺序排列他们的名字并在新行上打印每个。
示例输入 0:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
示例输出 0:
Berry
Harry
代码
from __future__ import print_function
score_list = {}
for _ in range(input()):
name = raw_input()
score = float(raw_input())
if score in score_list:
score_list[score].append(name)
else:
score_list[score] = [name]
new_list = []
for i in score_list:
new_list.append([i, score_list[i]])
new_list.sort()
result = new_list[1][1]
result.sort()
print (*result, sep = "\n")
我无法理解这里的 "in" 函数,in
不会检查列表中的值,所以 score_list
不是空的吗?
我在代码中添加了注释以便更好地理解,希望对您有所帮助。
from __future__ import print_function
# Create an empty dict
score_list = {}
# Take a number input and run this loop that many times
for _ in range(input()):
name = raw_input()
score = float(raw_input())
# if value of score is an existing key in dict, add name to the value of that key
if score in score_list:
score_list[score].append(name)
else:
# Else create a key with score value and initiate the value with a list
score_list[score] = [name]
new_list = []
for i in score_list:
new_list.append([i, score_list[i]])
new_list.sort()
result = new_list[1][1]
result.sort()
print (*result, sep = "\n")
第一次字典是空的,第二次不是。我在每一行都添加了评论。
# Import
from __future__ import print_function
# Create a new dictionary to save the scores
score_list = {}
# For every input, do something
for _ in range(input()):
# Grab the name of the current user
name = raw_input()
# Grab the score of the current
score = float(raw_input())
# If the score is already in the list,
# append the name to that score
if score in score_list:
score_list[score].append(name)
# Else add the new score and name to the list
else:
score_list[score] = [name]
# Create a new list
new_list = []
# Copy score list into the new list
for i in score_list:
new_list.append([i, score_list[i]])
# Sort on score value
new_list.sort()
# Return the second highest score
result = new_list[1][1]
# Sort the names of the result
result.sort()
# Print it
print (*result, sep = "\n")
我是 python 的新手,正在尝试通过在 hackerrank 上练习来学习。我不理解这个列表概念。这就是问题
输入格式:
第一行包含一个整数, ,学生人数。 随后的行描述了每个学生的行;第一行包含学生姓名,第二行包含他们的成绩。
约束条件
总会有一个或多个学生的成绩倒数第二。
输出格式:
写出物理成绩倒数第二的所有学生的姓名;如果有多个学生,请按字母顺序排列他们的名字并在新行上打印每个。
示例输入 0:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
示例输出 0:
Berry
Harry
代码
from __future__ import print_function
score_list = {}
for _ in range(input()):
name = raw_input()
score = float(raw_input())
if score in score_list:
score_list[score].append(name)
else:
score_list[score] = [name]
new_list = []
for i in score_list:
new_list.append([i, score_list[i]])
new_list.sort()
result = new_list[1][1]
result.sort()
print (*result, sep = "\n")
我无法理解这里的 "in" 函数,in
不会检查列表中的值,所以 score_list
不是空的吗?
我在代码中添加了注释以便更好地理解,希望对您有所帮助。
from __future__ import print_function
# Create an empty dict
score_list = {}
# Take a number input and run this loop that many times
for _ in range(input()):
name = raw_input()
score = float(raw_input())
# if value of score is an existing key in dict, add name to the value of that key
if score in score_list:
score_list[score].append(name)
else:
# Else create a key with score value and initiate the value with a list
score_list[score] = [name]
new_list = []
for i in score_list:
new_list.append([i, score_list[i]])
new_list.sort()
result = new_list[1][1]
result.sort()
print (*result, sep = "\n")
第一次字典是空的,第二次不是。我在每一行都添加了评论。
# Import
from __future__ import print_function
# Create a new dictionary to save the scores
score_list = {}
# For every input, do something
for _ in range(input()):
# Grab the name of the current user
name = raw_input()
# Grab the score of the current
score = float(raw_input())
# If the score is already in the list,
# append the name to that score
if score in score_list:
score_list[score].append(name)
# Else add the new score and name to the list
else:
score_list[score] = [name]
# Create a new list
new_list = []
# Copy score list into the new list
for i in score_list:
new_list.append([i, score_list[i]])
# Sort on score value
new_list.sort()
# Return the second highest score
result = new_list[1][1]
# Sort the names of the result
result.sort()
# Print it
print (*result, sep = "\n")