Finding a substring within a list in Python
print [s for s in list if sub in s]
If you want them separated by newlines:
print "\n".join(s for s in list if sub in s)
Full example, with case insensitivity:
mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'
print "\n".join(s for s in mylist if sub.lower() in s.lower())
How to check if a string is a substring of items in a list of strings
To check for the presence of 'abc'
in any string in the list:
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in xs):
...
To get all the items containing 'abc'
:
matching = [s for s in xs if "abc" in s]
Finding a substring in a list of lists
You need to check if StreamCache
is part of any string in the list, which you could do with something like this:
[l for l in data if any('StreamCache' in s for s in l)]
If StreamCache
always occurs at the beginning of the string, this would be more efficient:
[l for l in data if any(s.startswith('StreamCache') for s in l)]
Find the list indices of all occurrences of a substring within the list
Try this:
indices = [idx for idx, s in enumerate(alphabet) if name in s]
where alphabet
is your list of strings, and name
is the desired substring.
Python: How to check a string for substrings from a list?
Try this test:
any(substring in string for substring in substring_list)
It will return True
if any of the substrings in substring_list
is contained in string
.
Note that there is a Python analogue of Marc Gravell's answer in the linked question:
from itertools import imap
any(imap(string.__contains__, substring_list))
In Python 3, you can use map
directly instead:
any(map(string.__contains__, substring_list))
Probably the above version using a generator expression is more clear though.
How to check for the presence of a substring in a list of strings
You have a numpy array, not a list.
Anyway, considering a list (this would also work on a numpy array):
my_lst = ['hello', 'this', 'is', 'a', 'testello']
query = 'ello'
out = [query in e for e in my_lst]
# [True, False, False, False, True]
for a numpy array:
my_array = np.array(['hello', 'this', 'is', 'a', 'testello'])
out = np.core.defchararray.find(my_array, query)>0
# array([ True, False, False, False, True])
Python list of substrings in list of strings
any(y in x for x in string_list for y in substrings)
How to find all occurrences of a substring?
There is no simple built-in string function that does what you're looking for, but you could use the more powerful regular expressions:
import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]
If you want to find overlapping matches, lookahead will do that:
[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]
If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:
search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]
re.finditer
returns a generator, so you could change the []
in the above to ()
to get a generator instead of a list which will be more efficient if you're only iterating through the results once.
Related Topics
Find the Indexes of All Regex Matches
Putting Many Python Pandas Dataframes to One Excel Worksheet
How to Find the First Key in a Dictionary
Get Column Index from Column Name in Python Pandas
How to Assign a Variable in an If Condition, and Then Return It
Python Curve_Fit with Multiple Independent Variables
Download File Using Partial Download (Http)
How to Set a Default Value for a Wtforms Selectfield
How to Get Md5 Sum of a String Using Python
Why Is 'Object' an Instance of 'Type' and 'Type' an Instance of 'Object'
Monkey Patching a Class in Another Module in Python
Getting Standard Errors on Fitted Parameters Using the Optimize.Leastsq Method in Python
Real World Example About How to Use Property Feature in Python
Pythonic Way to Combine For-Loop and If-Statement
Get Inserted Key Before Commit Session
Populate a Pandas Sparsedataframe from a Scipy Sparse Matrix
What Does "Error: Option --Single-Version-Externally-Managed Not Recognized" Indicate