我的代码有问题。我有一个嵌套列表,其中包含来自一个句子的字符串,每个句子都在一个列表中。现在我的问题是如何不重复前面的句子来逐个阅读这些句子。
with open('test','r') as f:
test_iterate = test.read()
sample = [['This', 'is', 'a', 'sample','sentence'],['Sample','sentence','it','is']]
for words in test_iterate:
print (words)
现在我不知道该怎么办。我想要的输出:
This is a sample sentence
1
2
3
A
B
C
Sample sentence it is
1
2
3
A
B
C
在测试文件中:
1
2
3
A
B
C
答案 0 :(得分:1)
尝试一下:
for words in sample:
print(' '.join(words))
for line in test_iterate:
print(line)
答案 1 :(得分:0)
您可以尝试:
for sentence in list_of_sentences:
print(" ".join(sentence))
它遍历您存储为列表的句子,并用空格将列表的每个元素(即单词)连接起来。