Categories
Python Answers

How to calculate the Euclidean distance with Python NumPy?

Sometimes, we want to calculate the Euclidean distance with Python NumPy.

In this article, we’ll look at how to calculate the Euclidean distance with Python NumPy.

How to calculate the Euclidean distance with Python NumPy?

To calculate the Euclidean distance with Python NumPy, we can use the numpy.linalg.norm method.

For instance, we write:

import numpy

a = numpy.array((1, 2, 3))
b = numpy.array((4, 5, 6))

dist = numpy.linalg.norm(a - b)
print(dist)

We create 3 NumPy arrays a and b.

Then we call numpy.linalg.norm with a - b to calculate the distance between points a and b.

Therefore, dist is 5.196152422706632.

Conclusion

To calculate the Euclidean distance with Python NumPy, we can use the numpy.linalg.norm method.

Categories
Python Answers

How to get the source code of a Python function?

Sometimes, we want to get the source code of a Python function.

In this article, we’ll look at how to get the source code of a Python function.

How to get the source code of a Python function?

To get the source code of a Python function, we can use the inspect.getsource method.

For instance, we write:

import inspect


def foo(arg1, arg2):
    a = arg1 + arg2
    return a


lines = inspect.getsource(foo)
print(lines)

We create the foo function that we want to get the code for into a string.

To do that, we call inspect.getsource with foo and assign the code string to lines.

Therefore, lines is:

def foo(arg1, arg2):
    a = arg1 + arg2
    return a

Conclusion

To get the source code of a Python function, we can use the inspect.getsource method.

Categories
Python Answers

How to sort a list by multiple attributes with Python?

Sometimes, we want to sort a list by multiple attributes with Python.

In this article, we’ll look at how to sort a list by multiple attributes with Python.

How to sort a list by multiple attributes with Python?

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.

For instance, we write:

import operator

l = [[12, 'tall', 'blue', 1], [2, 'short', 'red', 9], [4, 'tall', 'blue', 13]]
s = sorted(l, key=operator.itemgetter(1, 2))
print(s)

We have the l nested list that we want to sort by the 2nd and 3rd items in each nested list.

Then we call sorted with l and the key set to operator.itemgetter called with 1 and 2 to sort l and return the sorted list.

We then assign the returned list to s.

Therefore, s is [[2, 'short', 'red', 9], [12, 'tall', 'blue', 1], [4, 'tall', 'blue', 13]].

Conclusion

To sort a list by multiple attributes with Python, we can use the sorted function with the operator.itemgetter method.

Categories
Python Answers

How to rotate a list in Python?

Sometimes, we want to rotate a list in Python.

In this article, we’ll look at how to rotate a list in Python.

How to rotate a list in Python?

To rotate a list in Python, we can use the deque instance’s rotate method.

For instance, we write:

from collections import deque

items = deque([1, 2, 3])
items.rotate(1)
print(items)

We use the deque class with a list to create a deque.

Then we call rotate on it with 1 to move the last item to the first position.

Therefore, items is deque([3, 1, 2]) according to the console.

Conclusion

To rotate a list in Python, we can use the deque instance’s rotate method.

Categories
Python Answers

How to run the shell ‘cd’ command to change the working directory with Python?

Sometimes, we want to run the shell ‘cd’ command to change the working directory with Python.

In this article, we’ll look at how to run the shell ‘cd’ command to change the working directory with Python.

How to run the shell ‘cd’ command to change the working directory with Python?

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.

For instance, we write:

import os

path = './'
os.chdir(path)

to change the current working directory to path.

Conclusion

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.