简体   繁体   中英

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.

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. 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:

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

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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