Reputation: 111
I have looked at Finding the index of an item given a list containing it in Python
and I have not found a solution. I have a list I appended of 426 values and I am looking for the index of 'KORD' but it claims it is not in the list, when it is.
metar_txt = open("metar.txt", "r")
lines = metar_txt.readlines()
for line in lines:
if len(line) > 20:
stations = []
stations.append(line.split(' ')[0])
print stations.index('KORD')
metar_txt.close()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-34-9271d129d452> in <module>()
5 stations = []
6 stations.append(line.split(' ')[0])
----> 7 print stations.index('KORD')
8 metar_txt.close()
ValueError: 'KORD' is not in list
Upvotes: 4
Views: 127
Reputation: 180391
create the list outside the loop, you are only storing a single element in your list as stations = []
in the loop keeps creating an empty list and then you add a single element, repeating each iteration:
stations = []
for line in lines:
if len(line) > 20:
If you are calling index in the loop each time then unless you add the substring on the very first iteration then you are going to keep getting an index error, not sure what your goal is but I imagine indexing when the loop completes will work:
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') # outside loop
If you just want the index of where it would appear keep a count as you go only incrementing the count when if len(line) > 20
is True which would be exactly the same as trying to find the substring index in the list at the end of your loop:
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
Lastly if you are trying to keep some index record for multiple words you can use a dict so finding the index will be 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"])
If you want efficient lookups and to keep the order you can use an 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
So for st in stations:print(st)
will output the stations in the order added and stations["word"]
will give you the index.
Or using a genexp and str.partition as Jon commented:
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))
Or using itertools.count
with a single genexp:
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)
Upvotes: 4