A
A
Alexander Pupkin2015-09-30 17:11:00
Python
Alexander Pupkin, 2015-09-30 17:11:00

How to perform a comparison?

Guys tell me.
There is such a list:
[('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]
How to correctly compare 7 with 1, 5 with 2, and so on. (any numbers can be)
Something like in Chinese, if:

a = [('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]
if  a[0][0] != a[0][1]:
    print ("Не равны")
else:
    print ("Равны")
if  a[1][0] != a[1][1]:
    print ("Не равны")
else:
    print ("Равны")

Well, and the like ...
Is it possible to check everything in a cycle somehow? Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
DDDsa, 2015-09-30
@sakrab

a = [('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]

for a0, a1 in a:
    print('равны' if a0==a1 else 'не равны')

A
abcd0x00, 2015-09-30
@abcd0x00

>>> lst = [('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]
>>> [a == b for a, b in lst]
[False, False, False, True]
>>>

V
Vladimir Martyanov, 2015-09-30
@vilgeforce

for pair in a: - and then extract elements from the tuple and compare.

D
Daniil Kolesnichenko, 2015-09-30
@KolesnichenkoDS

Option 1

a = [('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]
[print('равны') for t in a if t[0] == t[1] else print('не равны')]

Option 2
a = [('7', '1'), ('5', '2'), ('8', '3'), ('4', '4')]
for t in a: print('равны' if t[0] == t[1] else 'не равны')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question