简体   繁体   中英

How two write two nested lists column wise in .txt file?

I have two list of lists looks like:

sentences = [['its', 'a', 'great', 'show'], ['nice', 'movie'], ['good', 'series']]
labels = [['O', 'O', 'O', 'B_A'], ['O', 'B_A'], ['O', 'B_A']]

I want to save these pair of lists column wise in txt file so each element pair should be separated by a white space, and each list pair should be separated by an empty line.

The desired output should be like:

its O
a O
great O
show B_A

nice O
movie B_A

good O
series B_A

I have tried this:

filename = 'data.txt'

with open(filename, 'w') as f:
    for sen in sentences:
        for lab in labels:
            line = sen + ' ' + lab
            f.write(line)

I have the following error:

TypeError: can only concatenate list (not "str") to list

Update : Using the first answer, I am trying to define a function which takes two nested lists and the new file name as follows:


def create_txt(ls1, ls2,file_name):
    with open(file_name, 'w') as f:
        for sen, lab in zip(ls1,ls2):
            for i, j in zip(sen, lab):
                f.write(f'{i} {j}')
            f.write('\n')
    return file_name

But it returns the provided file name as a string:

create_txt(sentences, labels,'data_n.txt')

Output: 'data_n.txt'

what is the logical problem I've done here?

Thanks in advance!

you can use csv module for this.

import csv

with open("file.txt", "w", newline="\n") as fp:
    writer = csv.writer(fp, delimiter=" ")
    for sentence, label in zip(sentences, labels):
        for i, j in zip(sentence, label):
            writer.writerow([i, j])
        fp.write('\n')

without using any additional modules

with open("file.txt", "w") as fp:
    for sentence, label in zip(sentences, labels):
        for i, j in zip(sentence, label):
            fp.write(f'{i} {j}\n')
        fp.write('\n')

Another working answer, slightly different, with some explanatory comments:

sentences = [['its', 'a', 'great', 'show'], ['nice', 'movie'], ['good', 'series']]
labels = [['O', 'O', 'O', 'B_A'], ['O', 'B_A'], ['O', 'B_A']]

filename = "data.txt"
outputstring = ""

# Construct the output string with zip.

# First we're zipping the elements of the source lists,
# which gives a sequence of pairs like this:
# (sentences[0], labels[0]), (sentences[1], labels[1]), etc.

# Then we iterate over that sequence and zip up the contents of
# each pair of lists in the same way, and concatenate those strings
# with the outputstring, followed by a single newline character.
# After that, an extra newline is added to break up the groups.

for sentence, label in zip(sentences, labels):
    for i, j in zip(sentence, label):
        outputstring += i + " " + j + "\n"
    outputstring += "\n"

# This removes the extra whitespace at the end.

outputstring = outputstring.rstrip()

# Finally, you can just write the string to your output file.

with open(filename, "w") as f:
    f.write(outputstring)

And here is a second example without using zip :

sentences = [['its', 'a', 'great', 'show'], ['nice', 'movie'], ['good', 'series']]
labels = [['O', 'O', 'O', 'B_A'], ['O', 'B_A'], ['O', 'B_A']]

filename = "data.txt"
outputstring = ""

# Check the length of each list of lists and make sure they're the same:

sentenceslen = len(sentences)
labelslen = len(labels)

if sentenceslen != labelslen:
    print("Malformed data!")
    raise SystemExit

# Iterate over both lists using their lengths to define the range of indices.

for i in range(sentenceslen):
    
    # Check the lengths of each pair of sublists and make sure they're the same:
    
    subsentenceslen = len(sentences[i])
    sublabelslen = len(labels[i])
    
    if subsentenceslen != sublabelslen:
        print("Malformed data!")
        raise SystemExit
    
    # Iterate over each pair of sublists using their lengths to define the range of indices:
    
    for j in range(subsentenceslen):
        
        # Construct the outputstring by using both indices to drill down to the right strings,
        # ending with newline:
        
        outputstring += sentences[i][j] + " " + labels[i][j] + "\n"
    
    # Break up groups with newline again:
    
    outputstring += "\n"

# Remove whitespace at end:

outputstring = outputstring.rstrip()

# Write outputstring to file:

with open(filename, "w") as f:
    f.write(outputstring)

I do not recommend actually using the code in the second example. It is unnecessarily complicated, but I include it for completeness and to illustrate how the use of the zip function above saves effort. The zip function also does not care if you feed it lists of different length, so your script won't crash if you try that but don't check for it; it'll spit out pairs of values up to the length of the smaller list, and ignore values after that for the larger list.

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