在Python中查找数组中的索引

4

我已经查看了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

你是否打印出数组以确保KORD前缀或后缀中没有任何会破坏匹配的内容?同时将stations=[]放在循环外。 - idjaw
检查 stations.index('KORD\n') 或者您能展示一下 lines 中的一行是什么样子吗? - rebeling
1个回答

4
在循环外创建列表,你只会将单个元素存储在你的列表中,例如在循环中创建一个空列表 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') # outside loop

如果您只想知道它出现的索引位置,请在进行计数时保持计数,当 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)

你实际上想要做什么? - Padraic Cunningham
stations数组很长,但包含4个字母的条目。我正在寻找列表中条目“KORD”的索引。 - Brian B.
我猜使用str.split和显式分隔符也存在问题 - 因此仍有可能在末尾得到\n - Jon Clements
@PadraicCunningham 确实... 但是以后可能会成为一个陷阱 :) (你现在应该知道我很追求严谨了,Padraic!) - Jon Clements
@JonClements,好的,已添加,感谢。我还添加了一个版本,使用itertools.count来生成单个genexp。 - Padraic Cunningham
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接