【问题标题】:Python 3 - Finding which index of a list contains a particular string as a substringPython 3 - 查找列表的哪个索引包含特定字符串作为子字符串
【发布时间】:2013-04-04 20:07:33
【问题描述】:

我正在编写以下代码:

def findLine(prog, target):
   for l in range(0, len(prog)-1):
      progX = prog[l].split()
      for i in range(0, len(progX)):
         if progX[i] == target:
            a = progX[i]

...但我需要一种方法来查找 prog 的哪个索引包含 a。该程序的示例输入是:

findLine(['10 GOTO 20', '20 END'], '20')

问题本身应该比我自己解释得更好:
定义一个函数 findLine(prog, target) 来执行以下操作。假设 prog 是一个包含 BASIC 程序的字符串列表,例如 getBASIC() 生成的类型;假设 target 是一个包含行号的字符串,它是 GOTO 语句的目标。该函数应返回索引 i(一个介于 0 和 len(prog)-1 之间的数字),使得 prog[i] 是其标签等于 target 的行。

示例输入/输出:如果您调用 findLine(['10 GOTO 20','20 END'],'10') 那么输出应该是 0,因为列表的第 0 项是标签为 10 的行。

那么,如何找到第一个包含 ans 作为子字符串的索引?提前感谢您的帮助。

【问题讨论】:

    标签: python list python-3.x


    【解决方案1】:

    在我看来你很亲密......

    def findLine(prog, target):
       for l in range(0, len(prog)):  #range doesn't include last element.
          progX = prog[l].split()
          #you probably only want to check the first element in progX
          #since that's the one with the label
          if progX[0] == target:
              return l  #This is the one we want, return the index
    
          #Your code for comparison
          #for i in range(0, len(progX)):
          #   if progX[i] == target:
          #      a = progX[i]
    

    这部分使用enumerate可以做得更好:

    def findLine(prog, target):
       for l,line in enumerate(prog):
          progX = line.split()
          if progX[0] == target:
              return l  #This is the one we want, return the index
    

    如果你真的感兴趣,这可以在 python 的 1 行中完成:

    def findLine(prog,target):
        return next(i for i,line in enumerate(prog) if line.split()[0] == target)
    

    这句话有很多内容,但这是一个相当普遍的习语。它使用带有“生成器表达式”的next 函数。

    【讨论】:

    • 你也可以用line.startswith(target)代替line.split()[0] == target
    • @gonz -- 我想过,但我不知道 BASIC 以及它如何处理语句标签。我看到的startswith 的问题是:1)我不知道标签前是否可以有空格,2)在第 100 行中搜索 10 END 将返回 True...
    【解决方案2】:

    你跳过了最后一行。

    Range 产生一系列直到但不包括第二个参数的所有内容:

    >>> list(range(5))
    [0, 1, 2, 3, 4]
    

    看看有五个值,但5 不是其中之一? (如果range的第一个参数是0,可以省略。)

    一种更 Pythonic 的方式来迭代某些东西但仍然能够知道索引是使用 enumerate:

    for index, line in enumerate(prog):
        line_parts = line.split()
        for part in line_parts:
            if part == target:
                # Figure out what to do here.
                # Remember that 'index' is the index of the line you are examining.
    

    【讨论】:

      【解决方案3】:
      def iterfind (iterable, function):
          for i in enumerate(iterable):
              if function(i[1]): return i[0]
          return -1
      
      def basic_linenumber_find (mylist, linenumber):
          return iterfind(mylist, lambda x: int(x.split()[0]) == linenumber)
      
      basic_linenumber_find(['10 GOTO 20', '20 END'], 10)
      

      【讨论】:

      • 我不喜欢这样的事情返回-1,因为-1 是一个完全可以接受的列表索引——但它不会给你想要的元素。
      • 好吧,如果你愿意,你可以做一些不同的事情,比如 throw IndexError。
      • 基本上我是为了与字符串 find() 方法相似。
      猜你喜欢
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2023-01-12
      • 2023-03-15
      • 2020-09-22
      相关资源
      最近更新 更多