Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
# created at Jun 23, 2017 21:04 by Nil-Zil
"""
Sort list d, e, f by the elements in a, b, c.
"""
from operator import itemgetter
import numpy as np
a = [7, 4, 6]
b = [2, 5, 9]
c = [1, 8, 3]
d = [11, 10, 12]
e = [17, 14, 18]
f = [16, 13, 15]
# Group a and d, b and e, c and f
ad = np.transpose([a, d])
be = np.transpose([b, e])
cf = np.transpose([c, f])
def sort_by_first(lst):
for i in range(3):
m, n, o = sorted([ad[i], be[i], cf[i]], key=itemgetter(0)) # Sort by element in a, b, c
[ad[i], be[i], cf[i]] = [m.copy(), n.copy(), o.copy()]
return ad, be, cf
# Ungroup new ad, be, cf
a = np.transpose(ad)[0]
d = np.transpose(ad)[1]
b = np.transpose(be)[0]
e = np.transpose(be)[1]
c = np.transpose(cf)[0]
f = np.transpose(cf)[1]
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.