https://github.com/vitroid/smartfind
A smarter find function
https://github.com/vitroid/smartfind
Last synced: 9 months ago
JSON representation
A smarter find function
- Host: GitHub
- URL: https://github.com/vitroid/smartfind
- Owner: vitroid
- Created: 2019-06-12T03:45:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T08:11:21.000Z (about 7 years ago)
- Last Synced: 2025-03-14T15:21:29.036Z (over 1 year ago)
- Language: Python
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A smarter find() function.
* Can search multiple values.
* Can search with a condition.
* Always returns multiple matches.
In [1]: from smartfind import sfind
In [2]: C = [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0]
In [3]: tuple(sfind(C, (1,)))
Out[3]: (1, 3)
In [4]: tuple(sfind(C, lambda x:x==1))
Out[4]: (1, 3)
In [5]: #Want the first item
In [6]: next(sfind(C, lambda x:x==1))
Out[6]: 1
In [7]: #Lookup multiple items
In [8]: list(sfind(C, lambda x:x in (1,3)))
Out[8]: [0, 1, 3, 9, 15, 17, 24, 25, 27]
In [9]: list(sfind(C, (1,3)))
Out[9]: [0, 1, 3, 9, 15, 17, 24, 25, 27]
In [10]: #Lookup from the bottom
In [11]: list(sfind(C, (1,3), fromend=True))
Out[11]: [-6, -8, -9, -16, -18, -24, -30, -32, -33]
In [13]: