Late reply, but the other answer and the linked duplicate question seems to have missed an important aspect of your question, namely that you want the value, not the absolute value, with the largest magnitude. In other words, in a list like [-10, 0, 5], you want the result to be -10, not 10.
To get the value with its original sign, you could (conceptually) follow these steps:
- Get a copy of your list where all values were converted to their absolute values
- Find the index of the value that has the maximum absolute value
- Return the value at that index
This can be done quite easily in numpy by using argmax, which returns the index of the maximum value from an array. Step-by-step, it would look like this:
# Create a numpy array from the list you're searching:
xs = np.array([-10, 0, 5])
# Get an array where each value is converted to its absolute value:
xs_abs = np.abs(xs)
# This gives [10, 0, 5]
# Get the index of the highest absolute value:
max_index = np.argmax(xs_abs)
# This gives 0
# Get the number from the original array at the max index:
x = xs[max_index]
# This gives -10
This can be done in a single line like so:
x = xs[np.argmax(np.abs(xs))]
Answer from Phlippie Bosman on Stack OverflowVideos
Late reply, but the other answer and the linked duplicate question seems to have missed an important aspect of your question, namely that you want the value, not the absolute value, with the largest magnitude. In other words, in a list like [-10, 0, 5], you want the result to be -10, not 10.
To get the value with its original sign, you could (conceptually) follow these steps:
- Get a copy of your list where all values were converted to their absolute values
- Find the index of the value that has the maximum absolute value
- Return the value at that index
This can be done quite easily in numpy by using argmax, which returns the index of the maximum value from an array. Step-by-step, it would look like this:
# Create a numpy array from the list you're searching:
xs = np.array([-10, 0, 5])
# Get an array where each value is converted to its absolute value:
xs_abs = np.abs(xs)
# This gives [10, 0, 5]
# Get the index of the highest absolute value:
max_index = np.argmax(xs_abs)
# This gives 0
# Get the number from the original array at the max index:
x = xs[max_index]
# This gives -10
This can be done in a single line like so:
x = xs[np.argmax(np.abs(xs))]
If you mean highest magnitude regardless of negative or positive sign, then you would take the maximum of the list resulting from taking the absolute value of each constituent list value. Does that make sense?
Here is an example:
numbers = [3, 5, 7, -18]
matrix=[]
for x in numbers:
matrix.append(abs(x))
max(matrix)
Your log10/floor code is perfectly readable, and its performance cost will likely be dwarfed by that of the string formatting you will subsequently be doing on your output.
However, suppose you were to really need the performance...
Note that log10(x) == log2(x) / log2(10) == log2(x) * 1/log2(10)
1/log2(10) is a constant
log2(x) can usually be performed cheaply in the integer pipeline on modern architectures using instructions such as CLZ or a bit twiddling hack, yielding a number between 0 and 63 for a 64-bit integer. That fits in 6 bits, leaving us up to 58 bits after the radix point usable for fixed point arithmetic in a 64-bit type.
So we can then use fixed-point arithmetic to find the log10:
unsigned long long integer_log10( unsigned long long _in )
{
unsigned long long log10fp6x58 = 0x134413509f79ff0llu; // (unsigned long long) (double(1llu<<58) / log2(10.0))
return (((integer_log2(_in)) * log10fp6x58)+(1llu<<57)) >> 58;
}
The implementation of integer_log2 is compiler/platform-dependent; e.g. on GCC/PowerPC, it's
unsigned long long integer_log2( unsigned long long _in )
{
return 63 - __cntlzd(_in);
}
This approach can be generalised for finding the logarithm of any base, simply calculate the appropriate constant as described above.
This is the most straightforward and simple method i can think of ... and maybe it will be a bit faster than computing the logarithm:
postfixes = {{1e12, "T"},
{1e9, "G"},
{1e6, "M"},
{1e3, "K"}}
for each postfix in postfixes{
if(x > postfix.value){
return (x / postfix.value) + postfix.letter;
}
}
return x;
abs(x-y) will do exactly what you're looking for:
In [1]: abs(1-2)
Out[1]: 1
In [2]: abs(2-1)
Out[2]: 1
Although abs(x - y) and equivalently abs(y - x) work, the following one-liners also work:
math.dist((x,), (y,))(available in Python β₯3.8)math.fabs(x - y)max(x - y, y - x)-min(x - y, y - x)max(x, y) - min(x, y)(x - y) * math.copysign(1, x - y), or equivalently(d := x - y) * math.copysign(1, d)in Python β₯3.8functools.reduce(operator.sub, sorted([x, y], reverse=True))
All of these return the euclidean distance(x, y).