0

Imagine I have this tuples.

T1 = (2, 1)
T2 = (2, 2)

So I want to know if each element for T1 is the same in T2. Like this

'This is false' if T1[0] != T2[0] or T1[1] != T2[1] else 'is True!'

is there any cool way to check this without going through each element of each tuple?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Sharki
  • 404
  • 4
  • 23
  • 1
    Just use `t1 != t2` – Mateen Ulhaq May 17 '20 at 04:53
  • `T1 == T2` would do it. – Klaus D. May 17 '20 at 04:54
  • @mateen-ulhaq Why did you reverse the question in the title. It does not fit the text anymore. – Klaus D. May 17 '20 at 04:57
  • @KlausD. What do you mean? The "if statement" has nothing to do with the actual question, which focuses on the condition within the if statement. – Mateen Ulhaq May 17 '20 at 04:59
  • @mateen-ulhaq The text asks for a equality ckeck, the original title as well. The code does a kind of `not (not p or not q)` not very readable but still an equality check. – Klaus D. May 17 '20 at 05:07
  • @KlausD. `(t1 != t2) == not (t1 == t2)`. Since the inner condition `t1[0] != t2[0] or t1[1] != t2[1]` can be directly replaced by `t1 != t2`, I think it's not the worst thing in the world to call it a condition that "checks for non-equality". Also, the example does only `(not p or not q)`. I'm not sure where you got the other `not`. Perhaps you meant `not (p and q)`. Though enjoyable, this discussion is a bit moot since the question is closed anyways. – Mateen Ulhaq May 17 '20 at 05:30

1 Answers1

3

A regular equality check will do this.

'This is false' if T1 != T2 else 'is True!'

Tuple comparisons work by comparing each element in the tuple. This is frequently useful for sorting according to multiple criteria, since you can arrange multiple columns of data in a tuple according to sorting priority and then simply sort the tuples.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • What if I want to check T1[0] with T1[1] and T2[0] with T2[1]? – Sharki May 17 '20 at 04:57
  • 1
    Then you'd have to do that explicitly. Two tuples that are inverted versions of each other are not considered identical, but nothing prevents you from individually comparing arbitrary elements (although if you find yourself doing that, it's a clue that your data is not organized well). – Samwise May 17 '20 at 04:58
  • 1
    I'm not as familiar with numpy, but that sounds like something you could either verify experimentally or find information on in the numpy documentation. – Samwise May 17 '20 at 05:11
  • Yeah, I'm searching for some method for that, however, thanks a lot! :) – Sharki May 17 '20 at 05:13