Reputation: 920
I have got a list of complex values which comprises of real and imaginary parts.
Is there any possible way I can sort these in order of increasing imaginary values?
Upvotes: 0
Views: 165
Reputation: 879641
In [65]: import operator
In [67]: data = [2+3j, 4+5j, 1+2j]
In [69]: sorted(data, key=operator.attrgetter('imag'))
Out[71]: [(1+2j), (2+3j), (4+5j)]
To plot the points using matplotlib:
from matplotlib import pyplot as plt
import numpy as np
data = np.array([2+3j, 4+5j, 1+2j])
x, y = data.real, data.imag
plt.scatter(y, x)
plt.show()
Upvotes: 4
Reputation: 1201
Assuming values
is the list, this should work:
sorted_valued = sorted(values, key = lambda x: x.imag)
Upvotes: 1