Create matrix
Create matrix of None
A = [[ None for _ in xrange(M)] for _ in xrange(N)]
Create matrix of random numbers
import random
A = [[ random.random() for _ in xrange(M)] for _ in xrange(N)]
Generator for lists of M random elements
import random
A = ([ random.random() for _ in xrange(M)] for _ in iter(int, 1))
for i in A:
print i
sleep(0.2)
Flatten list of lists (only one level of nesting)
import itertools
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
list(itertools.chain.from_iterable(list2d))
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Strip punctuation of string
s.translate(None, string.punctuation)
Permutations
From python lexical analysis docs
def perm(l):
# Compute the list of all permutations of l
if len(l) <= 1:
return [l]
r = []
for i in range(len(l)):
s = l[:i] + l[i+1:]
p = perm(s)
for x in p:
r.append(l[i:i+1] + x)
return r
Print literal curly-braces
p = "{{}} {}"
p.format(7)
Random string
taken from here
import string, random
N = 7
''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
cryptographically more secure version
''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
Show notifications on macos
From here
import os
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
notify("Title", "Heres an alert")