【问题标题】:The output of the code is in different order than expected代码的输出顺序与预期不同
【发布时间】:2020-03-22 21:54:18
【问题描述】:
n = int(input())  
a_name = []  
a_score = []  
for _ in range(n):  
  name = input()  
  score = float(input())  
  a_name.append(name)  
  a_score.append(score)  
a_score1 = set(a_score)  
a_score1.remove(min(a_score1))  
x = min(a_score1)  
for i in range(n):  
    if a_score[i]==x:  
        print(a_name[i]) 

对于以下代码输入是:
5
哈利
37.21
浆果
37.21
蒂娜
37.2
阿克里提
41
苛刻
39

我的输出:
哈利
浆果

预期输出:
浆果
哈利

我是编程界的新手,希望能帮我解决这个问题。 这是一个黑客等级问题,问题是找到具有第二个最低分数的名称。我能够通过 8/10 测试用例,但有 2 个测试用例在此解决方案中失败。我能够找到正确的名称(即得分第二低的名称),但是它的打印顺序错误)

【问题讨论】:

  • 嗨,Saloni,欢迎来到 SO!将元组转换为集合是否有特殊原因?
  • 欢迎来到 Stack Overflow!查看tourHow to Ask。请edit你的问题有一个描述性的标题。现在问题来了,预期的顺序是什么?你已经按照出现的顺序排列了它们,这显然是错误的。他们应该被排序吗?
  • 您好 Joseph,我仍在努力学习 Python。我试图找到第二个最小值并想删除第一个最小值。这是我的做法。如果有什么不必要的,请随时纠正
  • @wjandrea 我将编辑标题。嗯,问题没有说明是否需要排序。问题是找到第二个最低分数并打印第二个最低分数的人的名字。我成功地找到了正确的名称,但黑客等级解决方案的名称顺序与我打印的名称顺序不同。
  • 您使用两个列表而不是字典是否有特殊原因?

标签: python-3.x list for-loop output


【解决方案1】:

您可以先找到第二个最小值,然后使用列表解析和内置函数 zip 找到具有第二个最小值的名称:

second_minim = sorted(a_score)[1]
[n for n, s in zip(a_name, a_score) if s == second_minim]

输出:

['Harry', 'Berry']

根据您指出的requirements,与为什么应该是['Berry', 'Harry'] 而不是['Harry', 'Berry'] 相关:

如果有多个年级相同的学生,请按字母顺序排列他们的名字并将每个名字打印在新行上

所以你应该使用:

result = sorted(n for n, s in zip(a_name, a_score) if s == second_minim)

print(*result, sep='\n')

输出:

Berry
Harry

【讨论】:

  • 正是问题所在。输出为 Harry Berry,预期为 Berry Harry
  • @SaloniShah 为什么应该是Herry Berry?为什么顺序很重要?
  • 这是一个黑客等级问题,请查看问题链接。我不知道为什么顺序很重要,但我想这就是黑客排名的工作方式,可能是因为它是一个嵌套列表问题。 hackerrank.com/challenges/nested-list/problem
  • @SaloniShah 检查我的答案
  • 好吧,让我试试这个。我想我明白这里做了什么。但是你能解释一下吗?我以前没用过 zip()。
【解决方案2】:

因此,您可以将分数映射到带有字典的名称,而不是使用两个列表。像这样:


scores_dict = {'Harry': 37.21,
          'Berry': 37.21,
          'Tina': 37.2,
          'Akriti': 41,
          'Harsh': 39
          }

接下来移除所有匹配到起始最小值的键,这样我们就可以得到第二个最小值

x = min(scores_dict, key=scores_dict.get)
filtered_scores = {k:v for k,v in scores_dict.items() if v != scores_dict[x]}

然后我们抓取任何新最小值的所有键。

x = min(filtered_scores, key=scores_dict.get)
res = [key for key in filtered_scores if filtered_scores[key] == filtered_scores[x]]

完整代码

n = int(input)
scores_dict = {}
for _ in range(n):
    name = input()
    score = int(input())
    scores_dict[name] = score

# The loop will produce this dict
'''
scores_dict = {'Harry': 37.21,
          'Berry': 37.21,
          'Tina': 37.2,
          'Akriti': 41,
          'Harsh': 39
          }
'''

x = min(scores_dict, key=scores_dict.get)
filtered_scores = {k:v for k,v in scores_dict.items() if v != scores_dict[x]}

x = min(filtered_scores, key=scores_dict.get)
res = [key for key in filtered_scores if filtered_scores[key] == filtered_scores[x]]

print(res)

如果您真的想使用两个列表,可以这样做,但它会增加不必要的复杂性,从而增加潜在的错误。

字典只是关联(命名)数组(列表)。

【讨论】:

  • 如何使用 dict 找到第二个最低分数?
  • @SaloniShah 我没有看到第二个。对不起,我会编辑我的答案
【解决方案3】:
n = int(input())
a_name = []
a_score = []
for _ in range(n):
    name = input()
    score = float(input())
    a_name.append(name)
    a_score.append(score)
a_score1 = set(a_score)
a_score1.remove(min(a_score1))
x = min(a_score1)
y =[]
for i in range(n):
    if a_score[i]==x:
        y.append(a_name[i])
y.sort()
for j in range(len(y)):
    print(y[j])

我得到了这段代码的输出。谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2022-07-06
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多