简体   繁体   English

在python中将输入拆分为行

[英]Splitting input into lines in python

I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . 我正在尝试解决此问题http://www.spoj.pl/problems/PEBBMOV/ I think I have the right algorithm, that is not the point of this question. 我认为我有正确的算法,这不是这个问题的重点。 This problem has a weird input file to it. 这个问题有一个奇怪的输入文件。 The input for one test case should be of the form n a1 a2 a3 ...an. 一个测试用例的输入应采用n a1 a2 a3 ... an的形式。 (all ints) (所有整数)

The problem here is that , there are stray newline characters etc in between the a[i]s. 这里的问题是,在a [i]之间有一些换行符等。 I need to be able to skip such newlines and collect all a[i]s belonging to one test case at a place. 我需要能够跳过这样的换行符,并在一个地方收集属于一个测试用例的所有a [i]。 How do I know all this? 我怎么知道这一切? Well a string of WAs and Runtime Errors , plus some research on the forums. 一连串的WA和运行时错误,以及论坛上的一些研究。 I have the following python code to try to do this, except that i seem to be faltering at crucial places and just cant get it done. 我有以下python代码来尝试执行此操作,除了我似乎在关键位置步履蹒跚而无法完成任务。 I hope to have the appropriate input lines in the list lines[] at the end of the input reading. 我希望在输入读数的末尾在列表行[]中具有适当的输入行。

Could someone please tell me my mistake(s) here.? 有人可以在这里告诉我我的错误吗? Or suggest a better approach?. 还是建议一个更好的方法? Thanks in advance.. 提前致谢..

import sys
#data = sys.stdin.readlines()
#lines = inp.split('\n')
data = sys.stdin.read()
pos = 0
lno = 0

lines = []
while pos<len(data):
    while not data[pos].isdigit():
                   pos = pos + 1
num =data[pos]
print num
cur = pos + 1
numbers_collected = 0

x = [] # temp list
y = []
while numbers_collected < num:

    if cur<len(data) and data[cur].isdigit():
        y.append(data[cur])
        cur = cur + 1
        numbers_collected += 1
    else:
        if cur<len(data)and numbers_collected < num:
            cur = cur + 1
        else:
            break
print x
pos = cur
x.extend(y)
lines.extend(x)



for line in lines:
    print line

Does this help you answer your question? 这是否可以帮助您回答问题?

In [1]: s1 = "1\n2\n\n3\n\n\n4\n\n\n\n5\n\n\n\n\n6"

In [2]: s1
Out[2]: '1\n2\n\n3\n\n\n4\n\n\n\n5\n\n\n\n\n6'

In [3]: s1.splitlines()
Out[3]: ['1', '2', '', '3', '', '', '4', '', '', '', '5', '', '', '', '', '6']

In [4]: [elem for elem in s1.splitlines() if elem]
Out[4]: ['1', '2', '3', '4', '5', '6']

Without seeing example input it's difficult to answer the question. 没有看到示例输入,很难回答这个问题。 However, the SPOJ problem page does not provide example input, so the OP can't provide something that isn't available. 但是, SPOJ问题页面未提供示例输入,因此OP无法提供不可用的内容。

Something like this can help: 这样的事情可以帮助您:

numbers = map(int, sys.stdin.read().split())
#numbers = [1,2,3,4,5]

This is a quick and dirty solution because it treats all whitespace as separators. 这是一个快速而肮脏的解决方案,因为它将所有空白都视为分隔符。

The spoj problem says that each game has exactly one input line: spoj问题说每个游戏只有一个输入行:

for line in sys.stdin:
    n, *piles = map(int, line.split())
    assert len(piles) == n

Let's assume that there could be newlines in between numbers in the same game: 假设在同一游戏中数字之间可能存在换行符:

numbers = (int(s) for line in sys.stdin for s in line.split() if line.strip())
for n in numbers:
    piles = list(itertools.islice(numbers, n))
    assert len(piles) == n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM