在循环外创建列表,你只会将单个元素存储在你的列表中,例如在循环中创建一个空列表
stations = []
,然后添加单个元素,每次迭代都会重复此操作。
stations = []
for line in lines:
if len(line) > 20:
如果您在循环中每次调用索引,除非您在第一次迭代时添加子字符串,否则您将继续收到索引错误。我不确定您的目标是什么,但我想在循环完成时进行索引操作会起作用:
with open("metar.txt", "r") as metar_txt:
stations = []
for line in metar_txt:
if len(line) > 20:
stations.append(line.rstrip().split(' ')[0]
print stations.index('KORD')
如果您只想知道它出现的索引位置,请在进行计数时保持计数,当 if len(line) > 20
为真时才递增计数器,这与在循环结束时尝试在列表中查找子字符串索引完全相同:
with open("metar.txt", "r") as metar_txt:
stations = []
i = 0
for line in metar_txt:
if len(line) > 20:
w = line.rstrip().split(' ')[0]
if w == "KORD":
print(i)
i += 1
最后,如果您想为多个单词保留一些索引记录,可以使用字典,这样查找索引就是0(1):
with open("metar.txt", "r") as metar_txt:
stations = {}
i = 0
for line in metar_txt:
if len(line) > 20:
w = line.rstrip().split(' ')[0]
stations[w] = i
i += 1
print(stations["KORD"])
如果您想要高效的查找并保持顺序,可以使用一个 OrderedDict
:
from collections import OrderedDict
with open("metar.txt", "r") as metar_txt:
stations = OrderedDict()
i = 0
for line in metar_txt:
if len(line) > 20:
w = line.rstrip().split(' ')[0]
stations[w] = i
i += 1
因此,for st in stations:print(st)
将按添加的顺序输出站点,stations["word"]
将给出索引。
或者像Jon评论的那样使用genexp和str.partition:
from collections import OrderedDict
with open("metar.txt", "r") as metar_txt:
lines = (line.partition(' ')[0] for line in metar_txt if len(line) > 20)
stations = OrderedDict((el, idx) for idx, el in enumerate(lines))
或者使用单个生成器表达式与 itertools.count
:
with open("metar.txt", "r") as metar_txt:
from itertools import count
cn = count()
stations = OrderedDict((line.rstrip().split(' ')[0], next(cn))
for line in metar_txt if len(line) > 20)