I have the following line of code:
val = tuple(s for s in name_list if str(ngram) in s)
this will search a list 'name_list' and print all elements in the list which has the substring "ngram" in it. Python seems to have this powerful 'if in' magic, and it is beautifully compact.
I'm used to seeing loops and conditionals as:
for line in file:
and
if x == y:
So can someone explain the actual structure for these one liners.
The reason I ask is because in my particular case the list 'name_list' is a list of 60K+ elements. I want to return and get the heck out of my function as soon as ten substrings are found. So more specifically:
s
for s in name_list:
if str(ngram) in s:
if len(s) <= 10:
return true
it's the variable s in front that is existing alone that throws me off as to how to refer to it, and if s is simply one substring match in my case. Or a list which is appended to with each found substring then I convert to a tuple.
I'm going to need some serious psychological help here.