我是 python 新手,它处理列表中变量和变量数组的方式对我来说很陌生。我通常会将一个文本文件读入一个向量,然后通过确定向量的大小将最后三个复制到一个新的数组/向量中,然后使用 for 循环将最后一个大小为三个的复制函数复制到一个新数组中。
我不明白 for 循环在 python 中是如何工作的,所以我不能这样做。
到目前为止,我有:
#read text file into line list
numberOfLinesInChat = 3
text_file = open("Output.txt", "r")
lines = text_file.readlines()
text_file.close()
writeLines = []
if len(lines) > numberOfLinesInChat:
i = 0
while ((numberOfLinesInChat-i) >= 0):
writeLine[i] = lines[(len(lines)-(numberOfLinesInChat-i))]
i+= 1
#write what people say to text file
text_file = open("Output.txt", "w")
text_file.write(writeLines)
text_file.close()