Function adas.list_index_str
def list_index_str(inlist, substr, pos=None, complement=False)-
Given a list of strings return a list of indices which contain a specified substring.
Parameters
inlist:list- list of strings
substr:str- value for which the indices are required.
pos:int- the substring must occur at this position.
complement:bool- return the index of lines where the substring is not found.
Returns
index:int- list of indices. If there are no matches -1 is returned.
count:int- the number of matches.
Notes
This replicates an ADAS coding pattern from IDL. It is the equivalent of index = where(strpos(lines, substr) NE -1, count) index = where(strpos(lines, substr) EQ pos, count)
Version History
-
Martin O'Mullane, 16-11-2019
- First version
-
Martin O'Mullane, 08-04-2021
- Add complement switch to return non-matching indices.
-
Martin O'Mullane, 15-10-2022
- If a specific position is requested, check for all occurrences of substr before testing.
Example
>>> import adas as adas >>> lines=['first line', '23', 'Te (eV)', 'Tion (eV)', 'another line'] >>> ind, count = adas.list_index_str(lines, 'eV') >>> ind [2, 3] >>> count 2