Comparison on the basis of min function

2024/11/17 21:24:16

How exactly does the min function work for lists in python ?

For example,

 num = [1,2,3,4,[1,2,3]]num2 = [1,2,3,4,5]min(num,num2) 

gives num2 as the result. Is the comparison value based or length based ?

Answer

First thing - when comparing two lists with min, elements are compared in order. So it is comparing 1 with 1, 2 with 2... and 5 with [1,2,3].

Second, in python 2, unequal types are allowed to be compared, and give an "arbitrary, but consistent" ordering. Quoth the docs:

The operators <, >, ==, >=, <=, and != compare the values of twoobjects. The objects need not have the same type. If both are numbers,they are converted to a common type. Otherwise, objects of differenttypes always compare unequal, and are ordered consistently butarbitrarily.

...

(This unusual definition of comparison was used to simplify thedefinition of operations like sorting and the in and not in operators.In the future, the comparison rules for objects of different types arelikely to change.)

In cPython, at least, this comparison is equivalent to comparing the strings that represent their respective types. Therefore:

5 < [1,2,3]
Out[8]: True

because 'int' < 'list'. I believe this is an implementation detail, the arbitrariness of this ordering should be stressed.

Thankfully in python 3 this silliness is replaced with TypeError.

https://en.xdnf.cn/q/71135.html

Related Q&A

Python Pandas rolling aggregate a column of lists

I have a simple dataframe df with a column of lists lists. I would like to generate an additional column based on lists.The df looks like:import pandas as pd lists={1:[[1]],2:[[1,2,3]],3:[[2,9,7,9]],4:…

Easy way of overriding default methods in custom Python classes?

I have a class called Cell:class Cell:def __init__(self, value, color, size):self._value = valueself._color = colorself._size = size# and other methods...Cell._value will store a string, integer, etc. …

Return first non NaN value in python list

What would be the best way to return the first non nan value from this list?testList = [nan, nan, 5.5, 5.0, 5.0, 5.5, 6.0, 6.5]edit:nan is a float

How to subplot pie chart in plotly?

How can I subplot pie1 in fig, so it be located at the first position. this is how I am doing it but it doesnt work out import pandas as pdimport numpy as npimport seaborn as snsimport plotly.offline a…

Example of use \G in negative variable-length lookbehinds to limit how far back the lookbehind goes

In the pypi page of the awesome regex module (https://pypi.python.org/pypi/regex) it is stated that \G can be used "in negative variable-length lookbehinds to limit how far back the lookbehind goe…

Regex with lookbehind not working using re.match

The following python code:import reline="http://google.com" procLine = re.match(r(?<=http).*, line) if procLine.group() == "":print(line + ": did not match regex") els…

testing python multiprocessing pool code with nose

I am trying to write tests with nose that get set up with something calculated using multiprocessing.I have this directory structure:code/tests/tests.pytests.py looks like this:import multiprocessing a…

Python verify url goes to a page

I have a list of urls (1000+) which have been stored for over a year now. I want to run through and verify them all to see if they still exist. What is the best / quickest way to check them all and re…

Bokeh: Synchronizing hover tooltips in linked plots

I have two linked plots. When hovering, I would like to have a tooltip appear in both plots. I already use the linked selection with great success, but now I want to link the tooltips also.Below is an …

Pipe STDIN to a script that is itself being piped to the Python interpreter?

I need to implement an SVN pre-commit hook which executes a script that itself is stored in SVN.I can use the svn cat command to pipe that script to the Python interpreter, as follows:svn cat file://$R…