Python Forum
get positive number from a list if there's the same number but negative
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get positive number from a list if there's the same number but negative
#1
list_one = [-8, 8, 144, 17]

def test(lis):
    return min(lis, key=abs)

print test(list_one)
Output:
-8
i want to print the closest number to 0 from a list,
in this case, -8 and 8 are the two closest number to 0, but i get the first number of the list(-8), and i would like that if there is two numbers which are as close each other to 0 (so like in this case, -8 and 8) I'd like to get the positive number ( so 8 )
how could i do that , thanks

well, resolved doing this,

list_one = [-8, 8, 144, 17]
 
def test(lis):
    m = min(lis, key=abs)
    if m < 0:
        if m and m*-1 in lis:
            return m*-1
        else:
            return m
    else:
        return m

print test(list_one)
new output :
Output:
8
was there a simpler way ?
Reply
#2
Yes

list_one = [-8, 8, 144, 17]
min(filter(lambda x: x >= 0, list_one))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
i will look at it , thanks
Reply
#4
With the list = [-8, 144, 17]
I get -8 with first programm
and 17 with second program.
Reply
#5
hi heiner, this is weird, it's not the case for me,
with first program, i get -8
with second program, -8 too
Reply
#6
Take my list = [-8, 144, 17], not the original one.
Reply
#7
this is what i did :

list_one = [-8, 144, 17]
 
def test(lis):
    return min(lis, key=abs)
 
print test(list_one)
Output:
-8
----

list_one = [-8, 8, 144, 17]
  
def test(lis):
    m = min(lis, key=abs)
    if m < 0:
        if m and m*-1 in lis:
            return m*-1
        else:
            return m
    else:
        return m
 
print test(list_one)
Output:
-8
Reply
#8
Sorry, with second program, I meant this:

list_one = [-8, 144, 17]
print(min(filter(lambda x: x >= 0, list_one)))
Reply
#9
oh yes, you're right, it seems to work only for positives numbers, i finally used this way:


list_one = [-8, 8, 144, 17]
def test(lis):
    m = min(lis, key=abs)
    if m and -m in lis:
        return abs(m)
    else:
        return m

print test(list_one)
Output:
8
and with you're list:

list_one = [-8,  144, 17]
def test(lis):
    m = min(lis, key=abs)
    if m and -m in lis:
        return abs(m)
    else:
        return m

print test(list_one)
Output:
-8
Reply
#10
Why
    if m and -m in lis:
and not only:
    if -m in lis:
?

I think "if m" is always true (except 0).
Or did you mean:
if m in list and -m in list:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send SMS from my phone number aster 4 4,004 May-11-2025, 05:48 PM
Last Post: TheTechNexus
Question [redistribution] Reduce number + size of dependencies? Winfried 2 758 Jan-31-2025, 10:17 PM
Last Post: snippsat
  Syntax for Doubling a number ksp_802 3 804 Jan-12-2025, 07:04 PM
Last Post: ksp_802
  Printing the code line number arbiel 6 1,809 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Finding the price based on industry and number of transactions chandramouliarun 1 1,814 Jun-04-2024, 06:57 PM
Last Post: marythodge4
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 1,169 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  intering int number akbarza 1 1,121 Apr-28-2024, 08:55 AM
Last Post: Gribouillis
  negative memory usage akbarza 1 1,359 Apr-27-2024, 08:43 AM
Last Post: Gribouillis
Brick Number stored as text with openpyxl CAD79 2 5,490 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 1,399 Jan-27-2024, 05:23 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020