Skip to content

Instantly share code, notes, and snippets.

@andreisoriga
Last active February 20, 2017 04:39
Show Gist options
  • Save andreisoriga/d49c7d486844cce4530a to your computer and use it in GitHub Desktop.
Save andreisoriga/d49c7d486844cce4530a to your computer and use it in GitHub Desktop.
Data Structures and Algorithms
# Mapping Keys to Multiple Values in a Dictionary
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(4)
d = defaultdict(set)
d['a'].add(1)
d['a'].add(2)
d['b'].add(4)
# USAGE:
# Instead of this:
d={}
for key, value in pairs:
if key not in d:
d[key] = []
d[key].append(value)
# do this:
d = defaultdict(list)
for key, value in pairs:
d[key].append(value)
# Keeping the Last N Items
from collections import deque
def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in line:
yield line, previous_lines
previous_lines.append(line)
# Example use on a file
if __name__ == '__main__':
with open('somefile.txt') as f:
for line, prevlines in search(f, 'python', 5):
for pline in prevlines:
print(pline, end='')
print(line, end='')
print('-'*20)
# Example 1:
>>> q = deque(maxlen=3)
>>> q.append(1)
>>> q.append(2)
>>> q.append(3)
>>> q
deque([1, 2, 3], maxlen=3)
>>> q.append(4)
>>> q
deque([2, 3, 4], maxlen=3)
>>> q.append(5)
>>> q
# Example 2:
>>> q = deque()
>>> q.append(1)
>>> q.append(2)
>>> q.append(3)
>>> q
deque([1, 2, 3])
>>> q.appendleft(4)
>>> q
deque([4, 1, 2, 3])
>>> q.pop()
3
>>> q
deque([4, 1, 2])
>>> q.popleft()
4
# Filtering Sequence Elements
# using list cmprehension:
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
[n for n in mylist if n > 0] # [1, 4, 10, 2, 3]
# using generator expression:
pos = (n for n in mylist if n > 0) # <generator object <genexpr> at 0x1006a0eb0>
for x in pos:
print(x)
# using filter function:
values = ['1', '2', '-3', '-', '4', 'N/A', '5']
def is_int(val):
try:
x = int(val)
return True
except ValueError:
return False
ivals = list(filter(is_int, values))
print(ivals)
# Outputs ['1', '2', '-3', '4', '5']
# Keeping Dictionaries in Order
from collections import OrderedDict
d = OrderedDict()
d['foo'] = 1
d['bar'] = 2
d['spam'] = 3
d['grok'] = 4
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
for key in d: print(key, d[key])
# Sorting Objects Without Native Comparison Support
class User:
def __init__(self, user_id):
self.user_id = user_id
def __repr__(self):
return 'User({})'.format(self.user_id) ...
users = [User(23), User(3), User(99)]
from operator import attrgetter
sorted(users, key=attrgetter('user_id')) # [User(3), User(23), User(99)]
# Unpacking Elements from Iterables of Arbitrary Length
record = ('Dave', '[email protected]', '773-555-1212', '847-555-1212')
name, email, *phone_numbers = user_record
# name: 'Dave'
# email: '[email protected]'
# phone_numbers: ['773-555-1212', '847-555-1212']
*trailing, current = [10, 8, 7, 1, 9, 5, 10, 3]
# trailing: [10,8,7,1,9,5,10]
# current: 3
line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'
uname, *fields, homedir, sh = line.split(':')
# uname: 'nobody'
# homedir: '/var/empty'
# sh: '/usr/bin/false'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment