Created
July 8, 2014 05:57
-
-
Save d2207197/5fa1ee54576db2762a45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:192f3145316d32ba6d587243c4d167e475350328fd97a6bbcb57bc31a332a18c" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from collections import OrderedDict\n", | |
"OrderedDict??" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from itertools import islice\n", | |
"class sparselist(OrderedDict):\n", | |
" def __getitem__( self, key ):\n", | |
" if isinstance( key, slice ):\n", | |
" res = []\n", | |
" for k, v in self.items():\n", | |
" if k >= key.stop:\n", | |
" break\n", | |
" elif k >= key.start:\n", | |
" res.append(v)\n", | |
" return res\n", | |
" elif isinstance( key, int ):\n", | |
" return super().__getitem__(key)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 88 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sl = sparselist([(1, \"a\"), (5, \"b\"), (10, 'c')])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print(sl[1],sl[5]) \n", | |
"print(sl[1:5], sl[1:6], sl[5:11])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"a b\n", | |
"['a'] ['a', 'b'] ['b', 'c']\n" | |
] | |
} | |
], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!gist sparse_list.ipynb --update 5fa1ee54576db2762a45" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"https://gist.github.com/5fa1ee54576db2762a45\r\n" | |
] | |
} | |
], | |
"prompt_number": 92 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment