简体   繁体   English

如何跳过列表中的行并根据索引号添加到文件中? Python

[英]how to skip over lines in a list and add to a file based on index number? python

I have a list, with entries separated by a new line:我有一个列表,条目以新行分隔:

lines=[
 '  22414035.537   117786547.45218     -3116.294                                  \n',
 '                  22414038.860    22414035.186    87957488.29217     -2327.383  \n',
 '  20484531.215   107646935.64119     -1170.828                                  \n',
 '                  20484533.402    20484530.680    80385700.15618      -874.345  \n',
 '  22744037.211   119520718.50318      3083.940                                  \n',
 '                  22744039.645    22744037.355    89252483.05018      2302.858  \n']

I have another list:我还有另一个清单:

Indx=[1]

-note, this list can be filled by multiple entries. -注意,此列表可以由多个条目填充。

I want to iterate through the list and if the index of every two lines is equal to a value from the list, Indx, then i want to do nothing.我想遍历列表,如果每两行的索引等于列表中的一个值 Indx,那么我什么都不做。 If the indx of every two lines does not equal a value from Indx, then I want to add that respective line and the next line in the file to a new file.如果每两行的 indx 不等于 Indx 的值,那么我想将文件中的相应行和下一行添加到新文件中。

for example in this case, the new file would contain:例如,在这种情况下,新文件将包含:

   22414035.537   117786547.45218     -3116.294                                  
                   22414038.860    22414035.186    87957488.29217     -2327.383  
   22744037.211   119520718.50318      3083.940                                  
                   22744039.645    22744037.355    89252483.05018      2302.858  

The issue I am having at the moment is that i cannot skip to the next line in the list.我目前遇到的问题是我无法跳到列表中的下一行。 furthermore, my code is adding lines to the file even when the count does equal a value from the Indx list.此外,即使计数确实等于 Indx 列表中的值,我的代码也会向文件添加行。

heres my code:继承人我的代码:

EditedRinexObs=open("H:\Uni Work\EditedRinexObs.16o", "a")
for line in lines:
    if ('g') not in line:
        count=(0)
        it=iter(lines)
        for x in indx:
            if count != x:
                EditedRinexObs.writelines(line)
                EditedRinexObs.writelines("\n")
                it.next()
                EditedRinexObs.writelines(line)
                EditedRinexObs.writelines("\n")
                it.next()
                count=count+1
             elif count == x:
                it.next()
                it.next()
                count=count+1
EditedRinexObs.close()   

I hope that makes sense, I'm not really sure whats going on and couldn't find the answer in other question.我希望这是有道理的,我不确定发生了什么,也无法在其他问题中找到答案。

If you only want to count pairs, create a set of indexes and zip the lines grouping every two lines together only yielding lines whose index is not in the set:如果您只想计算对,请创建一组索引并将每两行分组在一起的行压缩,只产生索引不在集合中的行:

def pairs(l,inds):
    it = iter(l)
    st = set(inds)
    for ind, (a, b) in enumerate(zip(it, it)):
        if ind not in st:
            yield a,b

print(list(pairs(lines, Indx)))

Which would give you:这会给你:

[('  22414035.537   117786547.45218     -3116.294                                  \n',
  '                  22414038.860    22414035.186    87957488.29217     -2327.383  \n'),
 ('  22744037.211   119520718.50318      3083.940                                  \n',
  '                  22744039.645    22744037.355    89252483.05018      2302.858  \n')]

If you want to consider overlapping pairs, you could use the pairwise recipe but then you can get duplicates so you need to decide what you want to do if that is the case, it would be something like:如果您想考虑重叠对,您可以使用成对配方,但是您可以获得重复项,因此您需要决定在这种情况下要做什么,它会是这样的:

from itertools import izip, tee, count

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)




def pairs(l, inds):
    st = set(inds)
    pr, cn = pairwise(l), count(0)
    prev = 0
    for a, b in pr:
        i = next(cn)
        if i not in st:
            if i - 1 != prev:
                yield a
            yield b
            next(pr)
            prev = i

Here's a fairly simple way that doesn't need iter , although I must confess I like Padraic's solution.这是一种不需要iter的相当简单的方法,尽管我必须承认我喜欢 Padraic 的解决方案。 :) :)

I'll output to stdout, to keep things simple.我将输出到标准输出,以保持简单。 I've changed your Indx to indx to conform to usual Python conventions: simple variable names should begin with a lower-case letter;我已将您的Indx更改为indx以符合通常的 Python 约定:简单的变量名称应以小写字母开头; names that begin with an upper-case letter are used for classes.以大写字母开头的名称用于类。 I've also turned it into a set, since testing membership of a set is generally faster than testing membership of a list, although for very small lists the list is probably faster.我还把它变成了一个集合,因为测试集合的成员资格通常比测试列表的成员资格更快,尽管对于非常小的列表,列表可能更快。

import sys

lines = [
    '  22414035.537   117786547.45218     -3116.294                                  \n',
    '                  22414038.860    22414035.186    87957488.29217     -2327.383  \n',
    '  20484531.215   107646935.64119     -1170.828                                  \n',
    '                  20484533.402    20484530.680    80385700.15618      -874.345  \n',
    '  22744037.211   119520718.50318      3083.940                                  \n',
    '                  22744039.645    22744037.355    89252483.05018      2302.858  \n'
]

indx = set([1])

out = sys.stdout

for i in range(len(lines) // 2):
    if i not in indx:
        out.write(lines[2*i])
        out.write(lines[2*i + 1])            

output输出

  22414035.537   117786547.45218     -3116.294                                  
                  22414038.860    22414035.186    87957488.29217     -2327.383  
  22744037.211   119520718.50318      3083.940                                  
                  22744039.645    22744037.355    89252483.05018      2302.858  

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

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