-1

What is this python code doing?

min((2,3),(6,'f',1))

Output: (2, 3)

I am not able to follow the documentation.

Can someone explain why the output in (2,3) and not an error?

dot.Py
  • 5,007
  • 5
  • 31
  • 52
Shimano
  • 785
  • 1
  • 6
  • 13

2 Answers2

1

Because (2,3) < (6,'f',1)

Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one

While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.

JBernardo
  • 32,262
  • 10
  • 90
  • 115
-2

The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.

eguaio
  • 3,754
  • 1
  • 24
  • 38
  • 1
    In the general sense of lexographic, eguaio is correct. Lexicographic comparison simply means that you go element-by-element. In this case, whether the value is `2` or `21`, it's still the first element at the tuple level. – Arya McCarthy Apr 20 '17 at 18:12
  • Exactly what aryamccarthy says. I think that @Prune should ask for clarification before starting downvoting answers. HIS miss interpretation of lexicographic in the context of tuples was charged to me :) – eguaio Apr 20 '17 at 18:41
  • [Wikipedia](https://en.wikipedia.org/wiki/Lexicographical_order) includes the definition with which I'm familiar from programming and text editing. Python's function does not match this definition. If you have another authority you can cite that supports your answer, I'll be happy to retract my down-vote. I have a lot of experience, but I'm not a world authority. – Prune Apr 20 '17 at 20:37
  • The Wikipedia article you shared contains a section named "Cartesian products". Please, read it carefully and you will see that the definition there is exactly the one I used on python tuples (and I think that it is correct to think on python tuples as cartesian product tuples, don't you think?) As the article tells, the order used in Cartesian products inherits the order in each coordenate, exactly how the order in python tuples works, and exactly how I'm using the term. – eguaio Apr 21 '17 at 00:19
  • I know I'm right on this, but the point of my first comment was that you downvoted an answer on a interpretation of a term without asking first clarifications, or corrections. I honestly think that attitude is mean and goes against the spirit of this site, which is populated of kind and work people that is just trying to help, like I do. – eguaio Apr 21 '17 at 00:30
  • @Prune, the SO answer that is mentioned above as this duplicate (http://stackoverflow.com/a/5292332/2384183) tells explicitly that the order in tuples is lexicographic, and it's quoted from python docs. – eguaio Apr 21 '17 at 08:54
  • It says that the *comparison* is lexicographic, and that the *order* is "corresponding elements". As explained elsewhere in the source documents that are quoted, the comparisons are according to type: numeric comparison for numeric types, lexicographic for sequences, etc. – Prune Apr 21 '17 at 16:37
  • @Prune You _compare_ tuples to _order_ them. And that is the order I'm talking about. Please, could you explain to me what is your understanding of my phrase: " _minimum_ _tuple_ _with_ _respect_ _to_ _lexicographic_" _order_?. Because I think it is clear it is talking about the general lexicographic order **on** tuples, which is build on top of whatever order you have defined in the coordinates depending on their data-types. – eguaio Apr 21 '17 at 16:59
  • I explained by giving you the definition of "lexicographic". My only issue is that I believe that your answer is technically incorrect, both in common English usage and in the current paradigm. Hence the down-vote. – Prune Apr 21 '17 at 17:02
  • You didn't give any definition. You first showed with an example that integers are not ordered lexicographical, and then quoted a wikipedia article that explains clearly in a section that what I'm telling is right. Did you read the section of the article I told you? Let me ask you this: what is your interpretation of lexicographic order of tuples with integer coordinates? please, give me an example. – eguaio Apr 21 '17 at 17:13