打印嵌套列表中第二低分数得分者的姓名,并按 Python 中的字母顺序排列
Printing name of second lowest mark scorer in a nested list and arranging in alphabetical order in Python
if __name__ == '__main__':
arr = []
for _ in range(int(input())):
name = input()
arr.append(name)
score = float(input())
arr.append(score)
array = [arr[i:i+2] for i in range(0, len(arr), 2)]
marks = []
for j in range(0, len(array)):
marks.append(array[j][1])
marks = list(map(float, marks))
marks.sort()
seclow = marks[1]
for k in range(0, len(array)):
if (seclow == float(array[k][1])):
print(array[k][0])
**SAMPLE INPUT:**
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
**EXPECTED OUTPUT:**
BERRY
HARRY
**MY OUTPUT:**
HARRY
BERRY
唯一的问题是按字母顺序对分数最低的名字进行排序。我应该使用列表来通过测试用例。那么,代码应该做哪些改动呢?
通过此处查看:https://ide.geeksforgeeks.org/fMD7OgxYC7
而不是定义列表。将其存储在字典中。这将为您提供 space 优化的解决方案。然后按值对字典进行排序。添加字典中有分数的名字。
尝试:
import collections
if __name__ == '__main__':
arr = {}
for _ in range(int(input())):
name = input()
score = float(input())
arr[name] = score
dd = collections.defaultdict(list)
for k,v in arr.items():
dd[v].append(k)
x = sorted(dd.items())
sec_low = sorted(x[1][1])
for i in sec_low:
print(i)
输入:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
输出:
Berry
Harry
x:
[(37.2, ['Tina']),
(37.21, ['Harry', 'Berry']),
(39.0, ['Harsh']),
(41.0, ['Akriti'])]
不使用任何导入且代码更少,可以通过这种方式完成。
我还把它放到了一个函数中,只是为了让它更干净一点,这样你就可以在需要的时候在其他地方重用它。
def main():
scores_dict = {}
for _ in range(int(input())):
name, score = input(), float(input())
if score not in scores_dict:
scores_dict[score] = []
scores_dict[score].append(name)
# Delete the min score key and names so that we can grab the second min
del scores_dict[min(scores_dict.keys())]
# Get new min score.
min_score = min(scores_dict.keys())
# Sort new min score.
scores_dict[min_score].sort()
# Print each name in results
[print(i) for i in scores_dict[min_score]]
if __name__ == '__main__':
main()
处理重复的最低值和重复的次低值。
示例输入:{“hri”:6.2,“dav”:1.1,“asv”:1.1,“wrs”:12.3,“dup”:6.2,“awe”:43.2,“asw”:22.2 "asd":6.2}
输出:['asd', 'dup', 'hri'] 可以打印连接到 "\n"
def lowest_scores(names_scores):
lowest_score=min(names_scores.values())
lowscorers=[]
low_scorer_dict = { k : v for k,v in names_scores.items() if v != lowest_score}
second_lowest_score = min(low_scorer_dict.values())
for k,v in low_scorer_dict.items():
if v == second_lowest_score:
lowscorers.append(k)
return sorted(lowscorers)
if __name__ == '__main__':
arr = []
for _ in range(int(input())):
name = input()
arr.append(name)
score = float(input())
arr.append(score)
array = [arr[i:i+2] for i in range(0, len(arr), 2)]
marks = []
for j in range(0, len(array)):
marks.append(array[j][1])
marks = list(map(float, marks))
marks.sort()
seclow = marks[1]
for k in range(0, len(array)):
if (seclow == float(array[k][1])):
print(array[k][0])
**SAMPLE INPUT:**
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
**EXPECTED OUTPUT:**
BERRY
HARRY
**MY OUTPUT:**
HARRY
BERRY
唯一的问题是按字母顺序对分数最低的名字进行排序。我应该使用列表来通过测试用例。那么,代码应该做哪些改动呢?
通过此处查看:https://ide.geeksforgeeks.org/fMD7OgxYC7
而不是定义列表。将其存储在字典中。这将为您提供 space 优化的解决方案。然后按值对字典进行排序。添加字典中有分数的名字。
尝试:
import collections
if __name__ == '__main__':
arr = {}
for _ in range(int(input())):
name = input()
score = float(input())
arr[name] = score
dd = collections.defaultdict(list)
for k,v in arr.items():
dd[v].append(k)
x = sorted(dd.items())
sec_low = sorted(x[1][1])
for i in sec_low:
print(i)
输入:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
输出:
Berry
Harry
x:
[(37.2, ['Tina']),
(37.21, ['Harry', 'Berry']),
(39.0, ['Harsh']),
(41.0, ['Akriti'])]
不使用任何导入且代码更少,可以通过这种方式完成。 我还把它放到了一个函数中,只是为了让它更干净一点,这样你就可以在需要的时候在其他地方重用它。
def main():
scores_dict = {}
for _ in range(int(input())):
name, score = input(), float(input())
if score not in scores_dict:
scores_dict[score] = []
scores_dict[score].append(name)
# Delete the min score key and names so that we can grab the second min
del scores_dict[min(scores_dict.keys())]
# Get new min score.
min_score = min(scores_dict.keys())
# Sort new min score.
scores_dict[min_score].sort()
# Print each name in results
[print(i) for i in scores_dict[min_score]]
if __name__ == '__main__':
main()
处理重复的最低值和重复的次低值。
示例输入:{“hri”:6.2,“dav”:1.1,“asv”:1.1,“wrs”:12.3,“dup”:6.2,“awe”:43.2,“asw”:22.2 "asd":6.2}
输出:['asd', 'dup', 'hri'] 可以打印连接到 "\n"
def lowest_scores(names_scores):
lowest_score=min(names_scores.values())
lowscorers=[]
low_scorer_dict = { k : v for k,v in names_scores.items() if v != lowest_score}
second_lowest_score = min(low_scorer_dict.values())
for k,v in low_scorer_dict.items():
if v == second_lowest_score:
lowscorers.append(k)
return sorted(lowscorers)