简体   繁体   English

python readlines()每次创建一个新列表

[英]python readlines() creating a new list each time

So Basically im trying to take a file and break it down depending on where text is positioned in the file. 因此,基本上,我会尝试获取文件并将其分解,具体取决于文本在文件中的位置。

So I have a method that looks like this 所以我有一个看起来像这样的方法

def sort_file(f):
    #print(f.read())
    mnemonics = []
    mnemonic_line = []
    next(f)#skip the default group on the first line
    for line in f.readlines():
        mnemonic_line.append(line[0:3])
        mnemonic_line.append(line[5:8])
        #print(line[5:8])
        #print(line[9:69])
        #print(line[69:139])
        #print(line[139:190])
        mnemonics.append(mnemonic_line)
        #del mnemonic_line[:]
        pdb.set_trace()
        #print(line.split())
        #print(len(line))

I am trying to create a list of lists, where each line is one list and the whole file is a list of lines. 我试图创建一个列表列表,其中每一行是一个列表,整个文件是一个行列表。

So I think I understand what the problem is but im not sure how to solve it. 因此,我认为我了解问题所在,但不确定如何解决。 From what I can tell its that each time I go through and I add to the list i use my orginal list which grows each time. 据我所知,每次我浏览并添加到列表时,我都会使用原始列表,该列表每次都会增长。

(Pdb) print mnemonics
[['CTM', 'ABS']]
(Pdb) c
(Pdb) print mnemonics
[['CTM', 'ABS', 'CTM', 'ACA'], ['CTM', 'ABS', 'CTM', 'ACA']]
(Pdb) c
(Pdb) print mnemonics
[['CTM', 'ABS', 'CTM', 'ACA', 'CTM', 'ACC'], ['CTM', 'ABS', 'CTM', 'ACA', 'CTM', 'ACC'], ['CTM', 'ABS', 'CTM', 'ACA', 'CTM', 'ACC']]

As you can see my mnemonic_line list grows each time i loop through and re-adds itself at position 1,2,3,4... with the value at its current loop. 如您所见,每次我循环通过时,我的mnemonic_line列表都会增加,并将其自身重新添加到位置1,2,3,4 ...处,并将其值添加到当前循环中。

What I think i need to do is create a new list each time and add it to my list of lists so my output looks something like this 我认为我需要做的是每次创建一个新列表,并将其添加到列表中,这样我的输出看起来像这样

[['CTM', 'ABS'],['CTM', 'ACA'],['CTM', 'ACC']]

Any help on how to do this would be greatly appreciated :) 任何有关如何执行此操作的帮助将不胜感激:)

You are not clearing out (or re-initializing) mnemonic_line after you append it to mnemonics within the loop. 您还没有清理掉(或重新初始化) mnemonic_line你追加到后mnemonics内环路。 This is what I mean: 这就是我的意思:

for line in f.readlines():
    mnemonic_line.append(line[0:3])
    mnemonic_line.append(line[5:8])
    mnemonics.append(mnemonic_line)
    mnemonic_line = []

You just need to shift mnemonic_line = [] into the for loop: 您只需要将mnemonic_line = []移到for循环中:

def sort_file(f):
    #print(f.read())
    mnemonics = []
    next(f)#skip the default group on the first line
    for line in f.readlines():
        mnemonic_line = [] # <------
        mnemonic_line.append(line[0:3])
        mnemonic_line.append(line[5:8])
        mnemonics.append(mnemonic_line)
        pdb.set_trace()

Look at your code. 查看您的代码。 You're appending mnemonic_line to mnemonics every iteration, but you aren't clearing out mnemonic_line 您将在每次迭代后将mnemonic_line附加到mnemonics ,但您并未清除mnemonic_line

Look at this and then apply it to your code 看一下,然后将其应用于您的代码

>>> lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> a = [] # your mnemonics
>>> b = [] # your mnemonic_line
>>> for line in lol:
        b.append(line[:2])
        a.append(b)
>>> a
[[[1, 2], [4, 5], [7, 8]], [[1, 2], [4, 5], [7, 8]], [[1, 2], [4, 5], [7, 8]]]
>>> b
[[1, 2], [4, 5], [7, 8]]
>>> a = []
>>> b = []
>>> for line in lol:
        b = []
        b.append(line[:2])
        a.append(b)
>>> a
[[[1, 2]], [[4, 5]], [[7, 8]]]
>>> b
[[7, 8]]

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

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