InputFile循环处理两个数据

时间:2015-10-12 05:57:34

标签: python

所以我试图从文件“data.txt”中找到最好的两个候选人,然后打印出候选人的名字和他们的分数。最佳候选人是得分最小的候选人,因此负数是最重要的。该文件如下所示:

  • 哈利
  • 100
  • 40
  • 的Mac
  • 79
  • 哈德森
  • -150

最好的两位候选人是Kim和Hudson。所以程序应打印出来:

  

     

40

     

哈德森

     

-150

这是我到目前为止的代码:

name = infile.readline()

score = float(infile.readline())

name2 = infile.readline()
score2 = float(infile.readline())

bestCandidate = name
bestScore = score
bestCandidate2 = name2
bestScore2 = score2


while name != "":
    name = infile.readline()
    if name != "":
        score = float(infile.readline())
        if score < bestScore:
            bestCandidate = name
            bestScore = score
        if bestScore < bestScore2:
            bestCandidate = name
            bestScore2 = score

print(bestCandidate)
print(bestScore)
print(bestCandidate2)
print(bestScore2)


infile.close()

该文件不是打印出最好的两个,而是打印以下内容:

  

哈德森

     

-150

     

     

-150

2 个答案:

答案 0 :(得分:1)

实现这一目标的更好方法是

  1. 将两名候选人初始化为&#34; sentinel&#34;值
  2. 遍历元素
  3. 如果一个元素比最佳候选者更好(或者最好是空的)将其复制到第二个并更新最佳候选者
  4. 其他如果元素优于第二个最佳候选者(或第二个是空的)更新它
  5. 重要的是,如果找到x < best,则需要执行两项操作:second_best = bestbest = x,以避免丢失应该成为第二小的旧的最小值。

    在代码中:

    ...
    best = None
    second_best = None
    while True:
        name = f.readline()
        if not name: break
        score = f.readline()
        if not score: break
        score = float(score)
        if best is None or score < best[1]:
            second_best = best # This is the important point
            best = (name, score)
        elif second_best is None or score < second_best[1]:
            second_best = (name, score)
    ...
    

    如果您忘记在best中移动second_best的步骤,该程序将无法使用(2,10,1)这样的序列,因为答案将是(1,10) (1,2)。

答案 1 :(得分:0)

您可以放置​​continue,这样就不会更新bestScore2。 continue语句跳过循环的其余部分并再次执行while语句。

 infile = open('d.txt')
name = infile.readline()

score = float(infile.readline())

bestCandidate = name
bestScore = score
name2 = infile.readline()

score2 = float(infile.readline())

bestCandidate2 = name2
bestScore2 = score2

#exchange bestScore2 and bestScore if bestScore2 < bestScore
if bestScore2 < bestScore:
    bestScore, bestScore2 = bestScore2, bestScore
    bestCandidate, bestCandidate2 = bestCandidate2, bestCandidate


while name != "":
    name = infile.readline()
    if name != "":
        score = float(infile.readline())
        if score < bestScore:
            bestCandidate2, bestScore2 = bestCandidate, bestScore
            bestCandidate = name
            bestScore = score
            continue
        elif score > bestScore and score < bestScore2:
            bestCandidate2, bestScore2 = name, score



print(bestCandidate)
print(bestScore)
print(bestCandidate2)
print(bestScore2)


infile.close()

因此输出变为:

Hudson

-150.0
Kim

40.0

以下是使用heapq的更好方法。堆有他们维持订单的财产。在这里,您希望保持分数的顺序,因此您要在堆中存储(分数,名称)元组。所以,如果你弹出两次堆,你会得到两个最好的候选人。

import heapq

h = []
infile = open('input_data.txt')
name = infile.readline()

score = float(infile.readline())
heapq.heappush(h, (score, name))

while name != "":
    name = infile.readline()
    if name != "":
        score = float(infile.readline())
        heapq.heappush(h, (score, name))

infile.close()
bestScore, bestCandidate = heapq.heappop(h)
bestScore2, bestCandidate2 = heapq.heappop(h)

print(bestCandidate)
print(bestScore)
print(bestCandidate2)
print(bestScore2)