Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Created May 5, 2015 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/105ae70452053720a37d to your computer and use it in GitHub Desktop.
Save belltailjp/105ae70452053720a37d to your computer and use it in GitHub Desktop.
Enforce 1st column to be smaller than the 2nd
# Construct 2-column ndarray
>>> a = numpy.random.randint(0, 100, (10, 2))
>>> a
array([[29, 1],
[33, 36],
[79, 32],
[72, 52],
[30, 9],
[33, 85],
[72, 29],
[52, 1],
[60, 89],
[12, 49]])
# Swap column if 1st value is larget than the 2nd.
>>> a[a[:, 1] < a[:, 0]] = a[a[:, 1] < a[:, 0]][:, ::-1]
>>> a
array([[ 1, 29],
[33, 36],
[32, 79],
[52, 72],
[ 9, 30],
[33, 85],
[29, 72],
[ 1, 52],
[60, 89],
[12, 49]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment