Skip to content

Instantly share code, notes, and snippets.

@Averroes
Averroes / arg_to_tuple_or_dico
Last active August 29, 2015 14:18
python arg tuple dict function argument
En effet en plaçant "*arg" dans la définition de la fonction les arguments seront reçus sous forme d'un tuple.
En effet en plaçant "**arg" dans la définition de la fonction les arguments seront reçus sous forme d'un dictionnaire
@Averroes
Averroes / example.py
Created April 10, 2015 14:05
calculating with dictionaries
# example.py
#
# Example of calculating with dictionaries
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
@Averroes
Averroes / example.py
Created April 10, 2015 14:05
determine the top n items occurring in a list
# example.py
#
# Determine the most common words in a list
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
@Averroes
Averroes / example.py
Created April 10, 2015 14:06
extracting a subset of a dictionary
# example of extracting a subset from a dictionary
from pprint import pprint
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
@Averroes
Averroes / example.py
Created April 10, 2015 14:06
filtering list elements
# Examples of different ways to filter data
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
# All positive values
pos = [n for n in mylist if n > 0]
print(pos)
# All negative values
neg = [n for n in mylist if n < 0]
@Averroes
Averroes / example.py
Created April 10, 2015 14:07
finding out what two dictionaries have in common
# example.py
#
# Find out what two dictionaries have in common
a = {
'x' : 1,
'y' : 2,
'z' : 3
}
@Averroes
Averroes / example.py
Created April 10, 2015 14:07
finding the largest or smallest n items
# example.py
#
# Example of using heapq to find the N smallest or largest items
import heapq
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
@Averroes
Averroes / grouping.py
Created April 10, 2015 14:08
grouping-records-together-based-on-a-field
rows = [
{'address': '5412 N CLARK', 'date': '07/01/2012'},
{'address': '5148 N CLARK', 'date': '07/04/2012'},
{'address': '5800 E 58TH', 'date': '07/02/2012'},
{'address': '2122 N CLARK', 'date': '07/03/2012'},
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
{'address': '1060 W ADDISON', 'date': '07/02/2012'},
{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]
@Averroes
Averroes / example.py
Created April 10, 2015 14:08
implementing a priority queue
# example.py
#
# Example of a priority queue
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
@Averroes
Averroes / example.py
Created April 10, 2015 14:09
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