我是编程新手,也是 Python 新手。
我写了一个实现 unix 尾部的函数:
def tail(file):
strin = open(file, 'r')
lis = strin.readlines()
lastline = lis[-1]
return lastline
strin.close()
但我认为它在性能上不是最优的。
我该如何改进?
最佳答案
您可以使用 Collections.deque
中的这个食谱
def tail(filename, n=10):
'Return the last n lines of a file'
return deque(open(filename), n)
引用这个:- https://docs.python.org/2/library/collections.html#deque-recipes
关于python - python 的尾部。最佳性能实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355787/