There is a list

number1=[1,2,3] number2=[1,2,3] power1=[50,100,150] power2=[25,150,300] x1=zip(number1,power1) x2=zip(number2,power2) 

How do I compare two lists (50> 25, 100> 150, etc.), provided that i in number1 == i in number2. As a result, there should be a list consisting of two lists: number1, result (true / false)

  • one
    The current formulation of the problem is incomprehensible. Why do zip lists numberX and powerX be made? Judging by the example in brackets, you need to compare the numbers from the power lists, but for some reason you write about the number lists. Rephrase the question. - Sergey Gornostaev
  • items = [a > b for a, b in zip(power1, power2)] print(items) # [True, False, False] ? - gil9red
  • one
    list(map(lambda x, y: x > y, power1, power2)) - slippyk
  • one
    @slippyk, lambda x, y: x > y -> operator.gt - mkkik
  • one
    @ gil9red, a matter of taste. It seems to me that readability is on the contrary increased. - mkkik

2 answers 2

 power1=[50,100,150] power2=[25,150,300] result = [] for i in range(len(power1)): result.append(power1[i] > power2[i]) # список списков с индексом # result.append([i+1, power1[i] > power2[i]]) # чтобы получить [[1, True], [2, False], [3, False]] print result # [True, False, False] 

or

 print [q > w for q,w in zip(power1, power2)] # [True, False, False] 
  • loop in one line: result = [power1[i] > power2[i] for i in range(len(power1))] - gil9red
  • But what about simple examples without sugar? - Eugene Dennis
  • They are also needed, in principle, you can specify both versions: classic and sugar-pitoniky :) - gil9red
 number1=[1,2,3] number2=[1,2,3] power1=[50,100,150] power2=[25,150,300] x1=zip(number1,power1) x2=zip(number2,power2) 

Having the input two tuples from the lists [(1, 50), (2, 100), (3, 150)] and [(1, 25), (2, 150), (3, 300)] you can just write a loop for as follows:

 y=list() for i, j in zip(x1,x2): y.append((i[0],i[1] > j[1])) 

The output is the required tuple [(1, True), (2, False), (3, False)]

  • The code in the loop can be shortened to: y.append(i[1] > j[1]) , since the value of the boolean operator is bool. Yes, and you can immediately put the value of i[0] -> y.append((i[0], i[1] > j[1])) - gil9red
  • You are right, so it will be shorter) - sevnight