1

please look at the following code:

for l in range(len(cosine_scores)):
            for s in range(len(skill_index)):
                if l!=skill_index[s] :
                    if cosine_scores[l][skill_index[s]]>=0.80:
                        print(l)

How I can rewrite this code in the way that if condition satisfy for all s before printing l?

For example if I have

my_list=[[10,8],[8,,0,1,2,7],[6,15,8]]

for i in my_list:
    for j in i:
       if j>5:      # I don't know what I should add here to say if this condition is true for all j in i. 
         print(i)

The correct out put should be [10,8] and [6,15,8].

azro
  • 53,056
  • 7
  • 34
  • 70
  • What do you mean by "condition satisfy for all s before printing l"? Try describing this very precisely in words without worrying about python syntax. Do you want every element of `skill_index` to be equal to the current element of `cosine_curves`? – Code-Apprentice Jul 27 '21 at 16:57
  • For your second example, check out the `all()` function. – Code-Apprentice Jul 27 '21 at 16:58

4 Answers4

5

General case

  1. all method

    The 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)
    
  2. for/else

    The else block is called only of no break has been used during the iteration

    for 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)
azro
  • 53,056
  • 7
  • 34
  • 70
  • An alternative option, if your check is small, and your list is big, that may be faster is reverse this. have a "bad list" like `bad=[0,1,2,3,4,5]` and then use `if any(x in good for x in bad): check=False` and then `if check: print(good)` --- i'm not saying this is ideal, but sometimes working it backwards can save a ton of time. especially when your "bad" list is relatively smaller - You could also just do `if any(x in good for x in range(0,6)` -- to put it differently, this is looping through the smaller list instead of the larger list (which could be 10k lines in some situations) – Robert Cotterman Jul 27 '21 at 17:45
0

You can also use a boolean variable as switch:

my_list=[[10,8],[8,,0,1,2,7],[6,15,8]]

for i in my_list:
    
    ALLTRUE = True

    for j in i:
        if not j>5:
            ALLTRUE = False
    
    if ALLTRUE:
        print(i)      
            
fsimonjetz
  • 5,644
  • 3
  • 5
  • 21
0

One-liner solution using all() in a list comprehension:

result = [i for i in my_list if all(j > 5 for j in i)]
ettanany
  • 19,038
  • 9
  • 47
  • 63
-1

I usually solve problems like this with a boolean.

my_list=[[10,8],[8,0,1,2,7],[6,15,8]]

printIt = True

for i in my_list:
    for j in i:
        if j<5:   
            printIt = False
    if printIt:
        print(i)
    else:
        printIt = True
Ben Alan
  • 1,399
  • 1
  • 11
  • 27