![]() |
get positive number from a list if there's the same number but negative - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: get positive number from a list if there's the same number but negative (/thread-6129.html) Pages:
1
2
|
get positive number from a list if there's the same number but negative - haye - Nov-07-2017 list_one = [-8, 8, 144, 17] def test(lis): return min(lis, key=abs) print test(list_one) 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 : was there a simpler way ?
RE: get positive number from a list if there's the same number but negative - DeaD_EyE - Nov-07-2017 Yes list_one = [-8, 8, 144, 17] min(filter(lambda x: x >= 0, list_one)) RE: get positive number from a list if there's the same number but negative - haye - Nov-07-2017 i will look at it , thanks RE: get positive number from a list if there's the same number but negative - heiner55 - Nov-07-2017 With the list = [-8, 144, 17] I get -8 with first programm and 17 with second program. RE: get positive number from a list if there's the same number but negative - haye - Nov-07-2017 hi heiner, this is weird, it's not the case for me, with first program, i get -8 with second program, -8 too RE: get positive number from a list if there's the same number but negative - heiner55 - Nov-07-2017 Take my list = [-8, 144, 17], not the original one. RE: get positive number from a list if there's the same number but negative - haye - Nov-07-2017 this is what i did : list_one = [-8, 144, 17] def test(lis): return min(lis, key=abs) print test(list_one) ----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)
RE: get positive number from a list if there's the same number but negative - heiner55 - Nov-07-2017 Sorry, with second program, I meant this: list_one = [-8, 144, 17] print(min(filter(lambda x: x >= 0, list_one))) RE: get positive number from a list if there's the same number but negative - haye - Nov-07-2017 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) 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)
RE: get positive number from a list if there's the same number but negative - heiner55 - Nov-07-2017 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: |