Skip to content

Instantly share code, notes, and snippets.

@adagio
Last active December 19, 2018 03:28
Show Gist options
  • Save adagio/24d8f4c11f15ae736826bb58921b1fb3 to your computer and use it in GitHub Desktop.
Save adagio/24d8f4c11f15ae736826bb58921b1fb3 to your computer and use it in GitHub Desktop.
Sort Tuples
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"unsorted_coords = [\n",
" (2, 1),\n",
" (3, 2),\n",
" (4, 5),\n",
" (1, 2),\n",
" (3, 7)\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2), (2, 1), (3, 2), (3, 7), (4, 5)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coords = sorted(unsorted_coords)\n",
"coords"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(2, 1), (1, 2), (3, 2), (4, 5), (3, 7)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coords = sorted(unsorted_coords, key=lambda x: (x[1], x[0]))\n",
"coords"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment