legacy-wiki
Python
Recovered from the older tannerjc.net wiki snapshot dated January 23, 2016.
MAIN
Simple examples
- iterating two lists at the same time
nfc = [Packers, 49ers]
afc = [Ravens, Patriots]
for teama, teamb in zip(nfc, afc):
print teama + vs. + teamb
Packers vs. Ravens
49ers vs. Patriots
- iterate through list with index
teams = [Packers, 49ers, Ravens, Patriots]
for index, team in enumerate(teams):
print index, team
0 Packers
1 49ers
2 Ravens
3 Patriots
- list comprehension
numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 == 0]
- dictionary comprehension
teams = [Packers, 49ers, Ravens, Patriots]
print {key: value for value, key in enumerate(teams)}
{'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
- test for dictionary key
data = {'user': 1, 'name': 'Max', 'three': 4}
is_admin = data.get('admin', False)
- list comprehension
x = [1,2,3,4,5,6]
#First 3
print x[:3]
[1,2,3]
#Middle 4
print x[1:5]
[2,3,4,5]
#Last 3
print x[-3:]
[4,5,6]
#Odd numbers
print x[::2]
[1,3,5]
#Even numbers
print x[1::2]
[2,4,6]
- combinations with itertools
from itertools import combinations
teams = [Packers, 49ers, Ravens, Patriots]
for game in combinations(teams, 2):
print game
('Packers', '49ers')
('Packers', 'Ravens')
('Packers', 'Patriots')
('49ers', 'Ravens')
('49ers', 'Patriots')
('Ravens', 'Patriots')
articles
generic
- http://github.com/mellort/reddit_api
- http://pythonconquerstheuniverse.wordpress.com/category/python-debugger/
- http://www.infoworld.com/d/application-development/pillars-python-six-python-web-frameworks-compared-169442
- http://maxburstein.com/blog/python-shortcuts-for-the-python-beginner/
text analytics
- https://textblob.readthedocs.org/en/latest/quickstart.html#quickstart
- https://github.com/sloria/textblob-aptagger
- http://guidetodatamining.com/chapter-7/
- http://stackoverflow.com/questions/2923420/fuzzy-string-matching-algorithm-in-python
- http://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison
- https://github.com/e-loue/pyke