Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
54 Cards in this Set
- Front
- Back
regular expression replace
|
re.sub(pattern, replace, subject)
|
|
regular expression search
|
re.search(pattern, subject [, ...])
returns Match object or None Match.group() - array of matches |
|
string replace (not regex)
|
string.replace(search, replacement)
|
|
find substring
|
string.find(search)
|
|
iterate through dict
|
for key,value in dict.iteritems()
|
|
get next item from dictionary
|
dict.iterkeys().next()
dict.iteritems().next() also: items, keys, values |
|
merge dictionaries
|
dict1 = {...}
dict1.update({...}) |
|
dictionary from two lists
|
newDict = dict(zip(list1, list2))
|
|
sets - lists with no duplicates
|
set([a, a, b])
-> set([a, b]) |
|
some obvious list methods, flip a list around?
|
reverse, pop, push, count
|
|
some coooool list methods
|
list.remove(x), list.index(x), list.count(x)
|
|
comprehension example
|
fruit = [' banana', ' loganberry ', 'passion fruit ']
fruit2 = [f.strip() for f in fruit] >>> ['banana', 'loganberry', 'passion fruit'] |
|
more complex comprehension example
|
pis = [str(round(355/113.0, i)) for i in range(1,6)]
>>>['3.1', '3.14', '3.142', '3.1416', '3.14159'] |
|
more comprehensions
|
vec = [2, 4, 6]
vec2 = [3*x for x in vec if x > 3] >>>[12, 18] vec3 = [[x,x**2] for x in vec] >>>[[2, 4], [4, 16], [6, 36]] |
|
comprehension - iterate through two lists
|
vec1 = [2, 4, 6]
vec2 = [4, 3, -9] vec3 = [x*y for x in vec1 for y in vec2] >>>[8, 6, -18, 16, 12, -36, 24, 18, -54] |
|
nested comprehension
|
mat = [
... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] print([[row[i] for row in mat] for i in [0, 1, 2]]) >>>[[1, 4, 7], [2, 5, 8], [3, 6, 9]] |
|
iterate through two lists
|
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue'] for q, a in zip(questions, answers): ... print 'What is your {0}? It is {1}.'.format(q, a) |
|
format - more powerful than %
|
'What is your {0}? It is {1}.'.format(q, a)
#allows for reusing args |
|
sequence types
|
str, unicode, list, tuple, buffer, xrange
|
|
unicode string
|
u'abc'
|
|
return true if any element in the iterable is true
|
any(iterable)
|
|
convert to binary string
|
bin()
|
|
convert to bool
|
bool()
|
|
check if something is callable (class or function...)
|
callable()
|
|
declare a class method (the same as a static + member method?)
|
class C:
@classmethod def f(cls, arg1, arg2, ...): ... |
|
compare two vars
|
cmp()
|
|
return the list of Objects (not vars or member values) in the current local scope or in an object
|
dir([object])
|
|
dir() example
|
>>> import struct
>>> dir() # doctest: +SKIP ['__builtins__', '__doc__', '__name__', 'struct'] |
|
find the quotient and remainder of two values
|
divmod(a, b)
|
|
stop!
|
quit(), exit()
|
|
for overloading comarisons
|
object.__lt__(self, other)
object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) |
|
split
|
re.split('\W+', 'Words, words, words.')
>>>['Words', 'words', 'words', ''] |
|
regex - return non overlapping matches of a pattern in string
|
re.findall(pattern, string[, flags])
|
|
regex - return iterator yielding MatchObject fo all non overlapping matches of a pattern in a string
|
re.finditer(pattern, string[, flags])
|
|
camelize string
|
string.capwords(s[, sep])
|
|
string - uppercase first letter
|
str.capitalize()
|
|
string - to lowercase
|
str.lower()
|
|
str - all to uppercase
|
str.upper()
|
|
iterate through file system
|
import os
for fileName in os.listdir(directory): pass |
|
import a class using a string
|
__import__('filename')
|
|
make object callable
|
def __call__(self): pass
|
|
call a local/global function with string
|
globals()["myfunction"]()
locals()["myfunction"]() |
|
get len() of an object
|
__len__()
|
|
try an except
|
try:
pass except Exception: #or a Exc pass |
|
dict elements
|
dict.values()
dict.keys() |
|
call function with arguments list
|
wrapper(*args)
The * next to args means "take the rest of the parameters given and put them in a list called args". |
|
call member function
|
getattr(obj, 'str')
|
|
escape % when formating string
|
"%s%%" % str
|
|
random numbers within a range
|
print(random.randint(1,10))
//5 print(random.uniform(3.2, 6.7)) //3.4089421725853639 |
|
time
|
import time
time = time.time() |
|
access obj property by string
|
prop = getattr(obj, 'attr')
|
|
timestamp
|
import time
time - time.time() |
|
sort list!
|
newlist = sorted(l, key=lambda k: k['name'])
|
|
check if file exists
|
os.path.exists("bob.txt")
|