create a list outside the loop, you save only one item in your list, because the stations = []loop continues to create an empty list, and then you add one item, repeating each iteration:
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 True, , :
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
, , dict, 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"] .
genxp 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 xp:
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)