top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Finding the Min for positive and negative in python 3.3 list

+1 vote
493 views

For example:

a=[-15,-30,-10,1,3,5]

I want to find a negative and a positive minimum.

example: negative
print(min(a)) = -30

positive
print(min(a)) = 1
posted Mar 12, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
min(a)
and
min([e for e in a if e >=0]
answer Mar 12, 2013 by anonymous
min(a)
This does not return a negative minimum on input [1] (because there is none).

min([e for e in a if e >=0]
This does not return a positive minimum on input [0] (because there is none).



I would have said:

    pos_min = min(e for e in a if e > 0)
    neg_min = min(e for e in a if e < 0)

And then deal with the ValueError when there is no such minimum, as appropriate.
0 votes

try this:

min(a) => -30
min([n for n in a if i>0]) => 1

answer Mar 12, 2013 by anonymous
Similar Questions
0 votes

I have a list of a list of integers. The lists are long so i cant really show an actual example of on of the lists, but I know that they contain only the integers 1,2,3,4.

so for example. s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]

I am calculating the product, sum, max, min.... of each list in s2 but I get negative or 0 for the product for a lot of the lists. (I am doing this in ipython)

for x in s2: Â  Â  print(len = , len(x), sum = , sum(x), prod = , prod(x), max = , max(x), min = , min(x))
...
 (len = , 100, sum = , 247, prod = , 0, max = , 4, min = , 1) (len = , 100, sum = , 230, prod = , -4611686018427387904, max = , 4, min = , 1) (len = , 100, sum = , 261, prod = , 0, max = , 4, min = , 1)
 .....
 (prod =, 0, max =, 4, min =, 1) (prod =, 1729382256910270464, max =, 4, min =, 1) (prod =, 0, max =, 4, min =, 1)
.... 

Whats going on?

0 votes

Hi,

I have a list of arbitrary length, and I need to split it up into equal size chunks. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.

I was wondering if anyone had a good solution to this for lists of any length

This should work:

l = range(1, 1000)
print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ]

I was looking for something useful in itertools but I couldn't find anything obviously useful.

Appretiate your help.

+1 vote

For example gcd(3, -7) returns -1, which means that a co-prime test that would work in many other languages 'if gcd(x, y) == 1' will fail in Python for negative y.

And, of course, since -|x| is less than |x|, returning -|x| rather than |x| is not returning the greatest common divisor of x and y when y is negative.

+2 votes

How we can send mail with attachment in Python? Is it any prerequisite for it?

...