You can use
absandmapfunctions like thismyList = [2,3,-3,-2] print(list(map(abs, myList)))
Output
[2, 3, 3, 2]
Or you can use list comprehension like this
[abs(number) for number in myList]Or you can use list comprehension and a simple if else condition like this
[-number if number < 0 else number for number in myList]
You can use
absandmapfunctions like thismyList = [2,3,-3,-2] print(list(map(abs, myList)))
Output
[2, 3, 3, 2]
Or you can use list comprehension like this
[abs(number) for number in myList]Or you can use list comprehension and a simple if else condition like this
[-number if number < 0 else number for number in myList]
A list comprehension would also work:
>>> lst = [2,3,-3,-2]
>>> [abs(x) for x in lst]
[2, 3, 3, 2]
>>>
Absolute value of a value in List in Python - Stack Overflow
Using LARGE to return a sorted list of absolute values
Well, here's the extremely convoluted formula that did the trick, in case anyone's interested.
=INDEX($C$2:$C$17,SMALL(IF(LARGE(IF($B$2:$B$17="A",ABS($C$2:$C$17),""),ROW(1:1))=ABS($C$2:$C$17),SEQUENCE(ROWS($B$2:$B$17),1,1,1),""),1+SUMPRODUCT(--(IFERROR(ABS($G$1:G1),"")=LARGE(IF($B$2:$B$17="A",ABS($C$2:$C$17),""),ROW(1:1))))))More on reddit.com
Sort a column of values by their absolute value
Pulp constrains operations
The problem is with the loop you are using , Trying modifying it this way
def abs1(array):
for i in range(len(array)):
if array[i] < 0:
array[i] = array[i] * (-1)
print(array)
The reason is the loop you were previously using was just for accesing list elements but not giving you reference to change anything in list I solved it with using indexes on list.
Try :
import numpy as np
array= ...
np.abs(np.array(array))
numpy is very powerful and fast, learn to use it asap.
EDIT since the problem is how to do a function on each element of a list and wrap those results in a list of the same length :
Two solutions come to mind :
- hardcore python style :
[x if x<0 else -x for x in array]
the brackets say I want a list, inside I write the function and I say where to find the parameters to my function.
[abs(x) for x in array]
[f(x) for x in array] # for any defined f that can handle every element of array
- map(lambda x : f(x), array)
you create a mapping on the elements of array. To those elements, you apply lambda. lambda is a way to define a function locally, very locally.
list(map(lambda x : abs(x),array))
finally you make a list from the mapping, to retrieve the result. If you don't, it will stay as an abstract, the definition of a relation between array and something that is yet to be instantiated.
A cleaner example I think:
a=[['2.40', '1970-1990', 'Austria']]
b = []
for i in a[0]:
try:
b.append(abs(float(i)))
except:
b.append(i)
print(b)
[2.4, '1970-1990', 'Austria']
a = ['2.40', '1970-1990', 'Austria'] #your old list (with the extra [] removed, they seem not to have a point... If you need them you can easily edit the code appropriately)
b = [] #a new list with the absolute values
for i in range(0, len(a)): #going through each of the values in a
try:
b += [abs(float(a[i]))] #trying to take the absolute value and put it in the new list (I have the float() because it appears that your 2.40 is a string. If you have it as an actual integer or float (such as 2.40 instead of '2.40') you can just use abs(a[i])
except:
b += [a[i]] #if taking the absolute value doesn't work it returns the value on its own.
print(b)