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
#11
yes, sorry, i meant : "if m in lis and -m in lis:"
but anyways, you're right, i just need " if -m in lis: "
thanks for help
Reply
#12
You wrote in your first post:
   Is there a simpler way?

What do you mean with simple:
a. less code
b. code should run faster

If b:
   Your code runs twice over the list.
   I assume there is an algorithm
   that runs only once over the list.
Reply
#13
i wanted a shorter code but doing the same as the one i used firstly which was :
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)
now i have:

list_one = [-8, 8, 144, 17]
def test(lis):
    m = min(lis, key=abs)
    if -m in lis:
        return abs(m)
    else:
        return m
 
print test(list_one)
Reply
#14
def test(lis):
    m = min(lis, key=abs)
    return abs(m) if -m in lis else m
Reply
#15
My code loops through list once and seems to handle all cases (just returns [0] if there's a zero in there etc):
a=[-16,4,16,7,-4]
def getzeroclosest(a):
    negmax=None
    posmin=None
    for n in a:
        if n==0:
            return [0]
        elif n<0:
            if negmax==None:
                negmax=n
            if n>negmax:
                negmax=n
        elif n>0:
            if posmin==None:
                posmin=n
            if n<posmin:
                posmin=n
    if -negmax==posmin:
        return [posmin,negmax]
    elif -negmax<posmin:
        return [negmax]
    else:
        return [posmin]
print getzeroclosest(a)
Thanks for reading My code that I typed with My fingers on My keyboard. Yeah right, there's no self ownership in this totalitarian world, none of those things are mine, we're all slaves.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 366 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 339 Jan-27-2024, 05:23 PM
Last Post: Winfried
  cmath.rect accepts a negative modulus JMB 2 326 Jan-17-2024, 08:00 PM
Last Post: JMB
  Prime number detector Mark17 5 799 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 926 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  capturing multiline output for number of parameters jss 3 809 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 573 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,213 Jul-25-2023, 10:20 PM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,087 May-31-2023, 10:20 AM
Last Post: britesc

Forum Jump:

User Panel Messages

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