Sunday, February 15, 2015

Overview

Example code can be found here

Table of content

  1. Arrays
  2. Dicts
  3. Numbers
  4. Probability
  5. String
  6. Regex
  7. Other




1. Arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
x = list("4321")  # ["4", "3", "2", "1"]
x.append("val1")  # ["4", "3", "2", "1", "val1"]
x.pop()           # ["4", "3", "2", "1"]
x.pop(0)          # ["3", "2", "1"]
x.insert(0,4)     # ["4", "3", "2", "1"]
x.sort()          # ["1", "2", "3", "4"]
x.reverse()       # ["4", "3", "2", "1"]
x = x[::-1]       # ["1", "2", "3", "4"]
max(x)            # "4"

y = ["ab", "cde", "efgh", "ij"]
sorted(y, key = len)    # ['ab', 'ij', 'cde', 'efgh']
sorted(y, key = len, reverse = True)  # ['efgh', 'cde', 'ab', 'ij']




2. Dicts

1
2
3
4
5
6
7
8
dic = {"y": 6, "x": 5}
dic.keys()         # ["y", "x"]
dic.values()       # [6, 5]
dic["z"] = 4       # dic = {"y": 6, "x": 5, "z": 4}
dic.pop("z", None) # dic = {"y": 6, "x": 5}

dict( [("hello",1), ("thing",2), ("yay", 3)] )
# {'thing': 2, 'yay': 3, 'hello': 1} 




3. Numbers

1
2
3
4
int("4")          # 4
"{0:b}".format(4) # '101'
2**3              # 8 (2^3)
4**.5             # 2.0




4. Probability

1
2
3
4
5
6
7
8
9
import itertools

itertools.combinations(array,x)  # array choose x
for comb in itertools.combinations(array,x):
  # do stuff
  
itertools.permutations(arr, x)   # permutation
for comb in itertools.permutations(array,x):
  # do stuff




5. String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
x = raw_input()  # "A bcdefg  "

len(x)      # 10
x[0]        # "A"
sorted(x)   # [' ', ' ', ' ', 'A', 'b', 'c', 'd', 'e', 'f', 'g']

x.lower()   # "a bcdefg  "
x.upper()   # "A BCDEFG  "

x += str(5) # "A bcdefg  5"
x[:-1]      # "A bcdefg  "
x[2:4]      # "bc"         x[startIndex:endIndexExluded:step]

x.title()       # "A Bcdefg  "
x[0].isalpha()  # True
x[0].isdigit()  # False

x.split()       # ["A", "bcdefg"]
"".join( sorted(x) )  # "   Abcdefg"




6. Regex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import re

pattern = r'(\d{3})-\d{3}-\d{4}'
text = "Hello, my name is Goku. My number is 811-910-9324. My son's number is 643-948-2345"

phoneNumRegex = re.compile(pattern)
matches = phoneNumRegex.search(text)
matches.group()                     #"811-910-9324"

matches = re.search(pattern, text)
matches.group()                     #"811-910-9324"
matches.group(0)                    #["811", "643"] because we put parenthesis around the first \d

phoneNumRegex.findAll(text)         #["811-910-9324", "643-948-2345"]
re.findAll(pattern, text)           #["811-910-9324", "643-948-2345"]




7. Other

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ternery
5 if booleanValue else 6  # (booleanValue)? 5 : 6;

# generate new list from existing list
x = ['a', '1', 'b', '2', 'c', '3', 'd', '4']
x = [c for c in x if c.isalpha()]   # ['a', 'b', 'c', 'd']
x = [c for c in x if c.isdigit()]   # ['1', '2', '3', '4']

# get idx and value in loop
for idx, val in enumerate(array):

# sets
# https://docs.python.org/2/library/sets.html
s = set([1, 1, 3, 5, 6, 6, 4])      # set([1, 3, 4, 5, 6])
s.add(8)                            # set([1,3,4,5,6,8])
s.discard(3)                        # set([1,4,5,6,8])
s.remove(4)                         # set([1,5,6,8]) throws error if key not present
6 in s                              # True

Random Posts