This file contains 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
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 |
This file contains 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
# example.py | |
# | |
# Example of calculating with dictionaries | |
prices = { | |
'ACME': 45.23, | |
'AAPL': 612.78, | |
'IBM': 205.55, | |
'HPQ': 37.20, | |
'FB': 10.75 |
This file contains 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
# 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' | |
] |
This file contains 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
# 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 | |
} |
This file contains 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
# 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] |
This file contains 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
# example.py | |
# | |
# Find out what two dictionaries have in common | |
a = { | |
'x' : 1, | |
'y' : 2, | |
'z' : 3 | |
} |
This file contains 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
# 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}, |
This file contains 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
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'}, | |
] |
This file contains 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
# example.py | |
# | |
# Example of a priority queue | |
import heapq | |
class PriorityQueue: | |
def __init__(self): | |
self._queue = [] | |
self._index = 0 |
This file contains 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
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 |
OlderNewer