繁体   English   中英

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

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

我有两个列表列表,如下所示:

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

我想将这对列表按保存在txt文件中,因此每个元素对应该用空格分隔,并且每个列表对应该用空行分隔。

所需的 output 应如下所示:

its O
a O
great O
show B_A

nice O
movie B_A

good O
series B_A

我试过这个:

filename = 'data.txt'

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

我有以下错误:

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

更新:使用第一个答案,我试图定义一个 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

但它将提供的文件名作为字符串返回:

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

Output: 'data_n.txt'

我在这里做的逻辑问题是什么?

提前致谢!

您可以为此使用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')

不使用任何额外的模块

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

另一个可行的答案,略有不同,有一些解释性评论:

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)

这是不使用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)

我不建议实际使用第二个示例中的代码。 它不必要地复杂,但为了完整起见,我将其包括在内,并说明如何使用上面的zip function 节省精力。 zip function 也不关心您是否提供不同长度的列表,因此如果您尝试但不检查它,您的脚本不会崩溃; 它会吐出不超过较小列表长度的值对,然后忽略较大列表的值。

暂无
暂无

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

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