【问题标题】:What is the best way to write a code in python to read some sample input from a "txt" files?在 python 中编写代码以从“txt”文件中读取一些示例输入的最佳方法是什么?
【发布时间】:2012-11-04 19:36:54
【问题描述】:

我知道这是一个非常基本的问题,但我也是 python 环境的新手。我正在编写我的第一个程序(数据结构问题),我需要阅读一些输入测试用例。

输入:

The first line contains the number of test cases T. T test cases follow. 
The first line for each case contains N, the number of elements to be sorted. 
The next line contains N integers a[1],a[2]...,a[N].

约束:

1 <= T <= 5
1 <= N <= 100000
1 <= a[i] <= 1000000

示例输入:

2
5
1 1 1 2 2
5
2 1 3 1 2

我编写了一个以下程序来从文件中读取上述输入,但我确信这不是最好的方法,因为它包含很多 if-else 循环和 for 循环,这真的很糟糕大号inputs

sample = open('sample.txt')
first = sample.readline()
if len(first) > 5 or len(first) <1:
    print "Not correct input";
else:
    test = sample.readline
    for x in range(0,len(first)):
        second = sample.readline()
        if len(second) >100000 or len(second) < 1:
            print "wrong input";
        else:
            third = list()
            for y in range(0, len(third)):
                third.append(sample.readline()[:1])
        method_test(third)  #calling a method for each sample input

请建议我最好的解决方案。

【问题讨论】:

  • 你想做什么?看来您的阅读部分已关闭,您是否要限制输入?此外,看起来这两个ifs 总是会失败(不可能同时满足和条件)。也就是说,他们永远不会打印“输入错误”/“错误输入”。
  • 其实我只是想得到一个通用的解决方案来从每个约束的文件中读取示例输入。
  • if len(first) &gt; 5 and len(first) &lt;1 没有意义。你的意思可能是or

标签: python input python-2.7


【解决方案1】:

应该这样做:

with open('sample.txt') as sample:
    num_testcases = int(sample.readline())
    assert 1 <= num_testcases <= 5
    for testcase in range(num_testcases):
        num_elems = int(sample.readline())
        assert 1 <= num_elems <= 10000
        elems = map(int, sample.readline().split())
        assert len(elems) == num_elems
        assert all(1 <= elem <= 100000 for elem in elems)
        method_test(elems)

编辑:添加了有效性检查。

【讨论】:

  • 非常简洁,很好地指出了元素的数量甚至没有被使用。也许您应该添加一些关于您使用 intlen 的文字以使其成为完整的软件包?我认为您也没有检查元素上的 OP 边界。
  • 我可能会添加一个 assert len(elems) == num_elems 但是好的答案
  • 我应该如何检查约束?我的意思是第三个约束?
  • @AmitPal:正在添加第三个约束条件和我的 Internet 连接中断了几分钟——现在好了。
  • 是的,我明白了,谢谢@martineau
【解决方案2】:

首先。 len(x) 会告诉你输入的长度,所以如果你的输入行是“9”,len(line) 将是 1;如果您的输入行是“999”,len(line) 将是 3。您需要使用 int(line) 从输入文件中正确读取数字。

程序其余部分的逻辑看起来不正确-例如,您正在读取第一行(测试数),然后循环遍历该数字(这很好)-但您正在读取数字这个循环之外的值,这是错误的顺序。

我强烈建议您在阅读时打印出各种值,这样您就可以了解正在发生的事情,并更轻松地调试您的程序。

最后当你执行以下操作时:

        third = list()
        for y in range(0, len(third)):...

您正在创建一个空列表list(),然后从 0 循环到列表的长度(也为零)。所以循环实际上不会做任何事情。

【讨论】:

  • 感谢您回答@DNA,但您是否可以编写解决方案。这样我就可以理解了
  • 不,抱歉-您需要自己解决这个问题才能从中学到任何东西。不过,我会在我的答案中添加更多指针。
【解决方案3】:

类似这样的:

使用cycle()只读取第一行之后的交替行,循环的大小将是T值的两倍。

 from itertools import islice,cycle
 with open("data1.txt") as f:
    T = int(f.readline())
    if T != 0:
            cyc=islice(cycle((False,True)),T*2) 
            for x in cyc:
                if x or not f.readline():
                    print map(int,f.readline().split())

输出:

[1, 1, 1, 2, 2]
[2, 1, 3, 1, 2]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 2014-02-13
  • 2011-03-16
  • 2012-10-10
相关资源
最近更新 更多