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

如何跳过列表中的行并根据索引号添加到文件中?python,python,list,Python,List,我有一个列表,条目之间用新行分隔: lines=[ ' 22414035.537 117786547.45218 -3116.294 \n', ' 22414038.860 22414035.186 87957488.29217 -2327.383 \n', ' 20484531.215 107646935.64119 -1170.828

我有一个列表,条目之间用新行分隔:

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=[1]
-注意,此列表可以由多个条目填写

我想遍历列表,如果每两行的索引等于列表中的一个值Indx,那么我什么也不想做。如果每两行的indx不等于indx中的值,那么我想将相应的行和文件中的下一行添加到新文件中

例如,在本例中,新文件将包含:

   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  
我现在面临的问题是,我不能跳到列表中的下一行。此外,我的代码正在向文件添加行,即使计数确实等于Indx列表中的值

这是我的密码:

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

我希望这是有道理的,我不确定到底发生了什么,也找不到其他问题的答案

如果只想计算对,请创建一组索引,并将每两行分组的行压缩到一起,只生成索引不在该集中的行:

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)))
这将给你:

[('  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')]

如果你想考虑重叠对,你可以使用成对的配方,但是你可以得到复制品,所以你需要决定你想做什么,如果是这样的话,它会是这样的:

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

如果只想计算对,请创建一组索引,并将每两行分组的行压缩到一起,只生成索引不在该集中的行:

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)))
这将给你:

[('  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')]

如果你想考虑重叠对,你可以使用成对的配方,但是你可以得到复制品,所以你需要决定你想做什么,如果是这样的话,它会是这样的:

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

这里有一个相当简单的方法,不需要
iter
,尽管我必须承认我喜欢Padraic的解决方案。:)

我将输出到stdout,以保持简单。我已将您的
Indx
更改为
Indx
,以符合常见的Python约定:简单变量名应以小写字母开头;以大写字母开头的名称用于类。我还将其转换为一个集合,因为测试集合的成员资格通常比测试列表的成员资格快,尽管对于非常小的列表,列表可能更快

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])            
输出

  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  

这里有一个相当简单的方法,不需要
iter
,尽管我必须承认我喜欢Padraic的解决方案。:)

我将输出到stdout,以保持简单。我已将您的
Indx
更改为
Indx
,以符合常见的Python约定:简单变量名应以小写字母开头;以大写字母开头的名称用于类。我还将其转换为一个集合,因为测试集合的成员资格通常比测试列表的成员资格快,尽管对于非常小的列表,列表可能更快

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])            
输出

  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  

我认为
下一行
可能会有帮助。我不知道确切的语法,但是使用迭代器,您可以调用next()并跳过一个条目。在您的示例中:如果
indx
只包含1,为什么输出时要省略第3行和第4行?@ScottHunter:indx
中的每个数字都引用一对行,所以前两行有索引0,后两行有索引1,等等。如果我使用next()我得到了错误消息:TypeError:list对象不是迭代对象,我认为
next(lines)
可能会有所帮助。我不知道确切的语法,但是使用迭代器,您可以调用next()并跳过一个条目。在您的示例中:如果
indx
只包含1,为什么输出时要省略第3行和第4行?@ScottHunter:indx
中的每个数字都引用一对行,所以前两行有索引0,后两行有索引1,等等。如果我使用next()我得到错误消息:TypeError:list对象不是迭代器