【问题标题】:How do i move the offset of the 'index' method of 'list'我如何移动'list'的'index'方法的偏移量
【发布时间】:2017-05-23 14:23:01
【问题描述】:

我知道索引函数是这样工作的:

list = ['dog','cat','pizza','trump', 'computer', 'trump']
print list.index('trump')

输出将是 3。 但现在我想让他打印另一个“王牌”字符串, 在 2 个对象之后。但如果我会执行相同的命令:

print list.index('trump')

他将再次打印 3 - 他看到的第一个王牌。 那么我如何移动索引函数的“偏移量”,以便她在索引 5 中检测到另一个王牌? 非常感谢你们!!

【问题讨论】:

  • 您是否要打印字符串出现的所有索引?
  • 您可能会发现这很有用stackoverflow.com/questions/6294179/…
  • 还是最后一个?
  • 顺便说一句,不要使用变量名list,因为它会影响内置函数list()

标签: python list offset


【解决方案1】:

list.index 接受第二个开始参数:

>>> lst = ['dog','cat','pizza','trump', 'computer', 'trump']
>>> lst.index('trump') # 1st index
3
>>> lst.index('trump',4) # 2nd index
5
>>> lst.index('trump',lst.index('trump')+1) # 2nd index
5

或者,如果您想要所有索引,请使用列表推导:

>>> [idx for idx,item in enumerate(lst) if item == 'trump']
[3, 5]

【讨论】:

    【解决方案2】:

    只注意字符串出现的第一个索引,第二次将多余的参数传递给index:

    l = ['dog','cat','pizza','trump', 'computer', 'trump']
    i = l.index("trump")
    print(i)
    print(l.index("trump",i+1))
    

    我明白了:

    3
    5
    

    从内联文档中,您可以传递一个可选的开始和停止值:

    索引(...)

    L.index(value, [start, [stop]]) -> integer -- 返回值的第一个索引。 如果值不存在,则引发 ValueError。

    (在一般情况下,您必须通过 try/except ValueError 块保护您对 index 的调用,以防该值不在列表中并采取相应措施)

    请注意,start 可以为负数。如果为负数,则从列表末尾计算索引。

    【讨论】:

      【解决方案3】:

      如果您想要列表中某个条目的所有索引的列表,您可以执行以下操作:

      indices = [i for i, x in enumerate(list) if x == "key"]
      

      【讨论】:

        【解决方案4】:

        创建列表的缩短版本。

        list=['dog','cat','pizza','trump', 'computer', 'trump']
        print list.index('trump')
        list = list[list.index('trump')+1:]
        print list.index('trump')
        

        【讨论】:

          【解决方案5】:
          l = ['dog','cat','pizza','trump', 'computer', 'trump']
          i = [n for n, item in enumerate(l) if item == 'trump'] #find all indices
          
          # print the result
          for n in i:
             print n, l[n]
          
          3 trump
          5 trump
          

          【讨论】:

            【解决方案6】:

            试试这个......它就像魅力一样。

            实际上,在您的问题中,即使在 for 循环中,迭代也只执行一次,它不会工作(即使在使用 iter 和 next() 之后对我也不起作用)所以我尝试使用 while 然后我意识到这是迭代游戏所以将while 放入try 和while 1: 以开始迭代1 和索引方法进行线性搜索,并在第一个匹配项处停止。如果没有找到匹配项,则会引发ValueError 异常。

            代码

            list = ['dog','cat','pizza','trump', 'computer', 'trump']
            value = "trump"
            i = -1
            try:
                while 1: # Start with Iteration 1 never 0
                    i = list.index(value, i+1)
                    print(value,"at", i)
            except ValueError: # No matching item is found.
                pass
            

            输出

            trump at 3
            trump at 5
            

            【讨论】:

            • 找到了另一种在列表中获取公共字符串的最简单方法 #CODE for idx,val in enumerate(l): if val=='trump': print(idx,val) #OUTPUT 3 trump 5 trump
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-09-18
            • 1970-01-01
            • 1970-01-01
            • 2021-08-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多