我刚刚看到DSM向我的answer发送评论create a series of tuples using a for loop并强迫我想知道是否有任何理由使用fileObj.readlines()而不是将fileObj传递给列表。就我所见,两者都给出了相同的结果。唯一的区别是可读性,但考虑到两者具有相同的可读性,应该采用哪种方式?
考虑两个场景
#This will create a tuple of file lines
with open("yourfile") as fin:
tup = list(fin)
#This is a straight forward way to create a list of file lines
with open("yourfile") as fin:
tup = fin.readlines()
我试图计时,但它没有多大意义,因为它们都具有相似的性能。