Short Python Code to say “Pick the lower value”?

2019-03-24 17:07发布

问题:

What I mean is, I'm looking for really short code that returns the lower value. for example:

a=[1,2,3,4,5,6,7,8,9,10]
b=[1,2,3,4,5,6,7,8]
len(a) = 10
len(b) = 8
if (fill-this-in):
     print(lesser-value)

And I forgot to add that if b is lower than a, I want b returned - not len(b) - the variable b.

回答1:

print(min(a, b))


回答2:

You're not hugely clear about what you want, so some alternatives. Given the following two lists:

a = [1,2,3,4,5,6,7,8,9,10]
b = [1,2,3,4,5,6,7,8]

To print the shortest list, you can just do..

>>> print(min(a, b))
[1, 2, 3, 4, 5, 6, 7, 8]

To get the shortest length as an number, you can either min the len() of each list, or do len(min()) (both are identical, choose which ever you find most readable)..

>>> print(min( len(a), len(b) ))
# or..
>>> print(len( min(a, b) ))
8

To print the lowest value in either list, you can supply the list as a single argument to min()

>>> a.extend(b) # Appends b to a
>>> print a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8]
>>> print(min(a))
1

Finally, another possibility, the list that has the lowest values in total:

>>> max( sum(a), sum(b) )
55

To print the actual list with the highest sum(), you could either use the ternary operator, like..

>>> print a if sum(a) > sum(b) else b
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

..although I never really liked (or use) it, instead using the slight longer, regular if/else statements..

>>> if sum(a) > sum(b):
...     print a
... else:
...     print b
... 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


回答3:

If the length of the list is what makes it lower (not its values), then you actually want:

min(a, b, key=len)

which is only incidentally equivalent to

min(a, b)

in the given example.



回答4:

min() should accomplish what you need

print(min(a,b))


回答5:

heads up, min(a, b, key=len) only works in python 2.5 and up I think.

(it's not working on my macbook with python 2.4, but my linux server with 2.5 is fine)



回答6:

Is the following what you want?

if len(a) < len(b):
    print a
else:
    print b

Alternatively, if you want to use the ternary operator like @Andrew G. Johnson:

print a if len(a) < len(b) else b

PS. Remember that Python does not use braces for its blocks, and that its ternary operator is different from C-like languages.



回答7:

It seems this answer may now be out of date. I just had this same question and found this answer, but wasn't getting the results I expected. Turns out Min doesn't automatically return the shorter of the two lists (in 2.7). To get that you have to use the 'key' argument (introduced in 2.5)(emphasis added):

min(iterable[, key]) min(arg1, arg2, *args[, key]) Return the smallest item in an iterable or the smallest of two or more arguments.

If one positional argument is provided, iterable must be a non-empty iterable (such as a non-empty string, tuple or list). The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.

The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

Changed in version 2.5: Added support for the optional key argument

So in this example, although it seems to work (and still would in 2.7), it only does because the list of integers is the same. However if these were two different non-ordered lists then:

min(a,b) 

would return the list with the lowest first integer.

To be sure to get the shorter of two lists, use:

min(a,b, key=len)


回答8:

I don't know Python but for something like this I'd use a ternary operator.

print(length(a) < length(b) ? length(a) : length(b))

One thing to note about this that if they are equal it will print length(b)