两个如何在 .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 文件中保存这对列表 按列 ,因此每个元素对应该用白色分隔 space,每个列表对应该用一个空行分隔。
所需的输出应如下所示:
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
更新:使用第一个答案,我试图定义一个函数,它接受两个嵌套列表和新文件名,如下所示:
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
但它 returns 提供的文件名作为字符串:
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
函数如何节省工作量。 zip
函数也不关心你是否给它提供不同长度的列表,所以如果你尝试这样做但不检查它,你的脚本不会崩溃;它会吐出成对的值,直到较小列表的长度,并忽略较大列表之后的值。
我有两个列表列表如下所示:
sentences = [['its', 'a', 'great', 'show'], ['nice', 'movie'], ['good', 'series']]
labels = [['O', 'O', 'O', 'B_A'], ['O', 'B_A'], ['O', 'B_A']]
我想在 txt 文件中保存这对列表 按列 ,因此每个元素对应该用白色分隔 space,每个列表对应该用一个空行分隔。
所需的输出应如下所示:
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
更新:使用第一个答案,我试图定义一个函数,它接受两个嵌套列表和新文件名,如下所示:
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
但它 returns 提供的文件名作为字符串:
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
函数如何节省工作量。 zip
函数也不关心你是否给它提供不同长度的列表,所以如果你尝试这样做但不检查它,你的脚本不会崩溃;它会吐出成对的值,直到较小列表的长度,并忽略较大列表之后的值。