简体   繁体   中英

Python 3 - Finding which index of a list contains a particular string as a substring

I'm working on the following code:

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]

...but I need a way of finding which index of prog contains a. An example input for this program is:

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

The problem itself should explain better than myself:
Define a function findLine(prog, target) to perform the following. Assume prog is a list of strings containing a BASIC program, like the type generated by getBASIC(); assume target is a string containing a line number, which is the target of a GOTO statement. The function should return the index i (a number between 0 and len(prog)-1) such that prog[i] is the line whose label equals target.

Sample input/output: If you call findLine(['10 GOTO 20','20 END'], '10') then the output should be 0, since item 0 of the list is the line with label 10.

So, how do I find the first index that contains ans as a substring? Thanks in advance for any help.

It seems to me like you're close ...

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]

This part can be done better using 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

And if you're real interested, this can be done in 1 line in python:

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

There's a lot going on in that line, but it's a fairly common idiom. It uses the next function with a "generator expression".

You're skipping the last line.

Range produces a sequence of everything up to but not including the second parameter:

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

See how there are five values but 5 isn't one of them? (And if the first parameter to range is 0 , you can omit it.)

A more pythonic way to iterate over something but still be able to know the index is to use 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.
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM