在python中查找数组中的索引

时间:2015-09-20 19:55:35

标签: python indexing

我看过了 Finding the index of an item given a list containing it in Python

我还没有找到解决方案。我有一个附加了426个值的列表,我正在寻找'KORD'的索引,但它声称它不在列表中,当它是。

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

1 个答案:

答案 0 :(得分:4)

在循环外创建列表,你只在列表中存储单个元素,因为stations = []在循环中不断创建一个空列表,然后你添加一个元素,重复每次迭代:

stations = []
for line in lines: 
    if len(line) > 20:

如果你每次都在循环中调用index,除非你在第一次迭代时添加子字符串然后你将继续得到一个索引错误,不确定你的目标是什么,但我想在循环完成时索引工作:

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 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"]会为您提供索引。

或使用genexp和str.partition作为Jon评论:

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与单个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)