This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
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?
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.
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.
Related
This question already has answers here:
How does the max() function work on list of strings in python?
(6 answers)
Closed 2 years ago.
Python code:
print(max(['2020','4','70','5']))
I am getting the output as 70
I want to understand how max() works on strings.
Can anyone explain?
if you provide max() a string, it returns the highest alphabetical character in a string. So it will order based on alphabetical order descending.
As explained here https://www.geeksforgeeks.org/python-string-max/#:~:text=os._exit()-,Python%20String%20%7C%20max(),alphabetical%20character%20in%20a%20string.&text=Return%20value%3A,highest%20character%20in%20the%20string.
max() can be used in 2 ways
Finding the largest item in an iterable
max(iterable, *iterables, key, default)
Getting the largest number in a list
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
I think that what you want is this last one. If you also added a
print(largest_number)
It would throw you the output of 10.
This question already has answers here:
Finding the longest and the shortest lists within a list in Python [duplicate]
(3 answers)
Closed 3 years ago.
max((12,1),(1,),(2,1,2) : So is it a wrong format to check which tuple is the greatest respective to the number of values in the tuple?
Because only the first value of each tuple is considered in this case.
If you want to treat the longest tuple as the maximum, you need to specify a different key function:
>>> max((12,1),(1,),(2,1,2), key=len)
(2, 1, 2)
By default, tuples are compared lexicographically, meaning the second element of two tuples is only considered if the first elements are equal. (The empty tuple is less than or equal to any other tuple.)
This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 3 years ago.
Why type((1)) is int and not a tuple? Whereas type((1,)) gives tuple.
That's also an answer to the question why we should use commas while defining a tuple with one value. Because tuples are not like lists which is unique in a way that we define it (using squared brackets) we have to add the comma to the value. In the first one type((1)) inner paranthesis have no effect, so it's just a basic integer nothing else. Like when you define expressions in paranthesis to give them priority. Hope it helps :)
Python compiler treated (1) as 1 because of that it is showing as int. that is inbuilt behavior of python compiler.
>>> a = (1)
>>> print(a)
1
>>> a = (1,)
>>> print(a)
(1,)
This question already has answers here:
What is the __lt__ actually doing for lists [duplicate]
(2 answers)
Closed 4 years ago.
How come max([1,2,3], [1,1,4]) returns [1,2,3] not [1,1,4]?
I was asked this question in a class. I don't understand why it returns [1,2,3] and the logic behind it (even if it returns [1,1,4], I still don't understand what max() function does).
The function max will succeed in outputing a maximum as long as the provided arguments are comparable.
In this case, the first argument is greater than the second with regard to list-ordering.
>>> [1, 2, 3] > [1, 1, 4]
True
This question already has answers here:
How to sort python list of strings of numbers
(4 answers)
Closed 8 years ago.
I am using the following function in order to sort a list in an increasing order. However, while my function works for lists such as: [1,5,6,9,3] or [56,43,16,97,45], it does not work for lists of the form: [20,10,1,3,50].
In such cases, the computer seems to consider that 3>20 and 3>10 and 3 ends up right before 50 (second to last) in the "sorted" list I get. More precisely the result I get is: [1,10,20,3,50].
Here is my code:
def function_sort(L):
for j in range(len(L)):
min=j
for i in range(j+1,len(L)):
if L[i]<L[min]:
min = i
if(min != j):
L[j],L[min] = L[min],L[j]
print L
return L
Could anyone please explain me what is going on?
It sounds like your list consists of strings rather than integers, and you end up getting the elements sorted lexicographically.
By way of illustration, consider the following:
>>> 10 < 2
False
>>> '10' < '2'
True
To fix the issue, convert the elements to integers before sorting:
L = map(int, L)
P.S. I recommend against using min as a variable name since it shadows the built-in function min().