Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含一系列数字的列表。此数字列表对应于 .dat 文件中的一行。如何使用列表说[0,1,2,3,4,5,6,9,4]然后打印出 .dat 文件中每个数字对应的行。
[0,1,2,3,4,5,6,9,4]
您将这些行读入内存,读入一个列表,然后索引到该列表中:
with open('somefile') as fileobj: lines = list(fileobj) for index in indices: print lines[index]
file = [ l for l in open('file.name') ] for i in list: print file[i]