Skip to content

Instantly share code, notes, and snippets.

@stjordanis
Forked from karlafej/attrgetter_sort.ipynb
Created September 21, 2018 14:24
Show Gist options
  • Save stjordanis/153b852d804df4787bf55a607f17274d to your computer and use it in GitHub Desktop.
Save stjordanis/153b852d804df4787bf55a607f17274d to your computer and use it in GitHub Desktop.
Sort objects of the same class that don’t natively support comparison operations using operator.attrgetter
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from operator import attrgetter"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class Fruit:\n",
"\n",
" def __init__(self, name, price):\n",
" self.name = name\n",
" self.price = price\n",
" def __repr__(self):\n",
" return f\"Fruit({self.name}, {self.price})\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"fruits = [Fruit(\"pear\", 22), Fruit(\"apple\", 33), Fruit(\"apple\", 11), Fruit(\"banana\", 20)]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Fruit(apple, 11), Fruit(banana, 20), Fruit(pear, 22), Fruit(apple, 33)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, key=attrgetter(\"price\"))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Fruit(apple, 33), Fruit(apple, 11), Fruit(banana, 20), Fruit(pear, 22)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, key=attrgetter(\"name\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Fruit(apple, 11), Fruit(apple, 33), Fruit(banana, 20), Fruit(pear, 22)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(fruits, key=attrgetter(\"name\", \"price\"))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.64 µs ± 12.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"sorted(fruits, key=attrgetter(\"price\"))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.98 µs ± 105 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"sorted(fruits, key=lambda fruit: fruit.price)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment