Search code examples
pythonindexingany

Extract the index of the substring that satisfied the any( ) statement in Python


So right now I have a functioning code that appends a 1 to a given list if one substring (in a list of substrings) exists inside of another longer string.

For example, if I wanted to identify where a ball was hit in a baseball game and I use the following string as an input:

s = 'SMITH grounded out to 1b.'

I can simply use the any() function as follows to determine whether first base was involved on the play:

first = []
first_ids = ['1b', 'first base']

if any(idx in s for idx in first_ids):
        first.append(1)
    else:
        first.append(0)

However, let's say:

s = 'SMITH grounded into double play 1b to ss to p.'

Now, we have the situation where multiple positions are involved and I would only like to include the first position. The process for identifying ss (shortstop) and p (pitcher) are exactly the same as 1b. My thought is to simply find which position is first by identifying the index of the substring that satisfied the any() function. Is there a simple way of doing so?


Solution

  • I think the best way to go for you is to keep a list of all the positions, split your data, and then filter:

    positions = ['1b', '2b', '3b', 'ss', 'p', 'c', 'lf', 'rf', 'cf']
    
    s = 'SMITH grounded into double play 1b to ss to p.'
    
    string_positions = [i for i in s.strip('.').split() if i in positions]
    
    print string_positions
    
    print string_positions[0]
    

    Output:

    ['1b', 'ss', 'p']
    
    '1b'