I have an issue and can't solve it, I have a dynamic list data
(request data from web) that contains multiple lists, and each one contains strings, integers, etc., but I need the one which contains a specific text StreamCache
. and just one and only one list in data
contains the string StreamCache
, which I store it in a new list. Almost all time my code works perfectly, but when it finds a list with a string like StreamCache@abnsdj12
or StreamCache*mljsgfn525
, that are essentially the lists I need, my code doesn't work, just because StreamCache
doesn't match exactly with StreamCache@kahsgsgh5
or so, I tried with list comprehension, regular expressions, but nothing works. Can someone help me?
These are my solutions:
# Works only if 'StreamCache' matchs exactly with the iterable
temp1 = [i for i in data if 'StreamCache' in i]
################ Solution 2 that doesn't work at all
search = 'StreamCache'
for element in data:
if isinstance(element, list):
new = [i for i in element]
z = re.compile('|'.join(re.escape(k) for k in new))
result = re.findall(z, search)
Hope you can help me with this.
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)]