How use `if` and `for` in the same line of code? [duplicate]
Solution 1:
General case
-
all
methodThe
all
Return True if all elements of the iterable are true (or if the iterable is empty)for i in my_list: if all(j > 5 for j in i): print(i)
-
for/else
The
else
block is called only of nobreak
has been used during the iterationfor i in my_list: for j in i: if not j > 5: break else: print(i)
Your case
for l in range(len(cosine_scores)):
for s in range(len(skill_index)):
if not (l != skill_index[s] and cosine_scores[l][skill_index[s]] >= 0.80):
break
else:
print(l)
---
for l in range(len(cosine_scores)):
if all(l != skill and cosine_scores[l][skill] >= 0.80 for skill in skill_index):
print(l)