简体   繁体   English

两个如何按列写入两个嵌套列表 in.txt 文件?

[英]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.我想将这对列表按保存在txt文件中,因此每个元素对应该用空格分隔,并且每个列表对应该用空行分隔。

The desired output should be like:所需的 output 应如下所示:

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:更新:使用第一个答案,我试图定义一个 function ,它采用两个嵌套列表和新文件名,如下所示:


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.您可以为此使用csv模块。

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 :这是不使用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.它不必要地复杂,但为了完整起见,我将其包括在内,并说明如何使用上面的zip function 节省精力。 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; zip function 也不关心您是否提供不同长度的列表,因此如果您尝试但不检查它,您的脚本不会崩溃; it'll spit out pairs of values up to the length of the smaller list, and ignore values after that for the larger list.它会吐出不超过较小列表长度的值对,然后忽略较大列表的值。

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

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