Access a repeatable item in a list and print the item next to it

I have this function that takes 3 arguments. 1) a list containing strings, 2) search_term, and 3) location (optional).

code:

def ls_src(list,search_term,place=1):
    if search_term in list:
        a = list[list.index(search_term)+1]+':' + '\tThe second element of of search term is ' + (search_term[place])
        return a

      

Now I want to access the item next to the search_term, but if the item is repeated in the list, it must also account for other occurrences of that item, not just the first occurrence of that item.

If list_search(['a','b','c','a','e'],'a')

then the function should return "b" and "e" both, since they are elements next to "a".

So my question is, how do we access other occurrences of 'a', not just the first occurrence.

+3


source to share


4 answers


You can create a new list using an iterator and a function next()

.

def list_search(input_list, search_term, place=1):
    terms = iter(input_list)
    new_list = []
    try:
        [new_list.append(terms.next()) for term in terms if term == search_term[place-1]]
    except StopIteration:
        pass
    return new_list


tests = [
    (['a','b','c','a','e'], 'a', 1),
    (['a','b','c','a','e'], 'b', 1),
    (['a','b','c','a','e'], 'ab', 2),
    (['a','b','c','a','e'], 'e', 1),
    (['a','a','a'], 'b', 1),
    (['a','a','a'], 'a', 1)]

for input_list, search_term, place in tests:
    print list_search(input_list, search_term, place)

      



These tests will give you the following results:

['b', 'e']
['c']
['c']
[]
[]
['a']

      

0


source


You need to use a function enumerate

that helps you get the item along with it.

def list_search(l, s):
    for i,j in enumerate(l):
        if j == s:
            print(l[i+1])

list_search(['a','b','c','a','e'],'a')  

      

Output:

b
e

      



or

Probably the search element may also be present last, so put a print statement inside the block try

except

.

def list_search(l, s):
    for i,j in enumerate(l):
        if j == s:
            try:
                print(l[i+1])
            except IndexError:
                pass    

list_search(['a','b','c','a','e', 'a'],'a') 

      

+4


source


If you prefer more descriptive code, you can use this approach. It's a little longer, but you are avoiding symbolic variables.

Another aspect that this provides is that the query string follows on its own, it will not be returned. This can be changed by removing the last tag if

.

def search_terms(terms, query):
    found = []
    count = len(terms)
    for index, term in enumerate(terms):
        next_index = index + 1
        if term == query and next_index < count and terms[next_index] != query:
            found.append(terms[next_index])
    return found

print search_terms(['a', 'a', 'b', 'c', 'a', 'e', 'a'], 'a')
# ['b', 'e']

      

+1


source


Code:

def search(l,term):

    for num in range(len(l)):

        if l[num]==term:

            print (l[num+1])

      

search (['python', 'html', 'python', 'C ++', 'python', 'java'], 'python')

Output:

html

c++

java

      

0


source







All Articles