InputFile循环处理两个d

2024-06-11 10:43:33 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想从档案中找出最好的两个候选人”数据.txt“然后打印出候选人的名字和分数。最好的候选人是得分最小的人,所以负数很重要。文件如下所示:

  • 哈利
  • 100个
  • 40个
  • Mac公司
  • 79个
  • 哈德逊
  • -150个

最好的两位候选人是金和哈德逊。所以程序应该打印出来:

Kim

40

Hudson

-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()

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

Hudson

-150

Kim

-150


Tags: 文件namereadlineiffloatinfilescoreprint
2条回答

您可以放置一个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的更好方法。堆具有维持秩序的特性。这里需要保持分数的顺序,因此在堆中存储(score,name)的元组。所以,如果你两次爆料,你就得到了两个最好的候选人。你知道吗

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)

更好的实现方法是

  1. 将两个候选值初始化为“sentinel”值
  2. 在元素上循环
  3. 如果一个元素优于最佳候选元素(或者最佳候选元素为空),则将其复制到第二个元素并更新最佳候选元素
  4. 否则如果元素优于第二个最佳候选元素(或第二个候选元素为空),则更新它

重要的一点是,如果您找到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)
...

如果您忘记了在second_best中移动best的步骤,程序将无法处理(2,10,1)这样的序列,因为答案将是(1,10)而不是(1,2)。你知道吗

相关问题 更多 >