Python3 Cheatsheet
Python3 Cheatsheet
Python3 Cheatsheet
Refer to https://www.programiz.com/python-programming/methods/built-in/any:
b = (“Jenny”, “Christy”)
The any() function returns True if any element of an iterable is True. If not, any() returns
x = zip(a, b)
False.
#use the tuple() function to display a readable version of the result:
>>> any([0,1])
#since 1 is True
True
https://www.w3schools.com/python/ref_func_zip.asp
#since 1 is True
>>> any([0,None])
2. sorted False
4. enumerate()
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5] This function is pretty handy when I want to get both index and value for a list (or key
and value for a dict).
In addition, there is an optional parameter “key” which serves as a function that can
take one input (it will be one element of the collection) and return whatever you want names = ['ant', 'maven', 'gradle']
print(full_name)
>>> sorted([5, 2, 3, 1, 4], key=lambda x:-x)
[5, 4, 3, 2, 1]
'''
maven-2.0
gradle-3.0
'''
Lambda Expression
In addition, I can add an optional parameter which specifies the starting index, an Lambda expression is actaully an anonymous function. It’s pretty handy when I need a
example from https://book.pythontips.com/en/latest/enumerate.html looks like: small function (one line of code) while I don’t want to take the trouble to write a
formal one.
def decreaseByTwo(x):
>>> f
[0, 0]
decreaseByTwo = lambda x: x-2 if x > 0 else x
x = decreaseByTwo(x)
OR
Lambda expression can be very useful wherever a function is needed to be a input
parameter.
>>> f = [ 0 ] * 2
>>> f
https://www.w3schools.com/python/python_lambda.asp
But per this link, careful — the second technique doesn’t generalize to
multidimensional arrays or lists of lists. Which leads to the List of lists changes Nested Function
reflected across sublists unexpectedly problem. Nested function is a function inside a function. It’s handy when you need to reuse a
batch of codes but that reuse only make sense to your current function. At that
b) to get a multidimensional array moment, you can create a nested function inside your current function.
>>> f
defined and can be invoked after its definition (but still inside parent()).
[[0, 0], [0, 0]]
>>> f[0][0]=3
Introducing nonlocal
>>> f
a = 3
objects inside a nest function, do I need to claim “nonlocal” first? The answer is not
b = 9 since, like child() in below example shows, changing part of variable a is not a new
def child():
assignment; but in child2(), I use “nonlocal” to completely change outside a instead of
print(a) #will print 3
partially.
print(b) #will print 9
child()
def parent():
a = {'parent':3}
However, there is a situation that child() may want to change the value of a variable def child():
print(a)
def child2():
def parent():
nonlocal a
a = 3 a = {1:2}
a = 5
child()
child2()
child()
print(a) #will print {1:2}
However, in above example, when a=5 is called, Python3 actually creates a local (only
valid within child()) variable which is also named ‘a’ and shadows the external a in Collections
following executions within child(). But outside child(), a will still refer to outside one. There are quite a lot of useful classes inside Python3 collection module. Per official
doc, collections module
To ensure the ‘a’ inside child() using the outside one, I need to put “nonlocal” as
below. implements specialized container datatypes providing alternatives to Python’s general
purpose built-in containers, dict , list , set , and tuple .
def parent():
What does that mean? Let me use 2 examples to explain:
a = 3
1. defaultdict
def child():
a = 5
m = {}
for c in s:
a[(1,2)]=(3,4)
if m.get(c) == None: #because c may not be an existing key in m
del a[(1,2)]
However, it’s a little bit clumsy. Let me show the defaultdict version:
For time complexity, adding or removing and accessing are all O(1) operations.
s = '123ab'
m = defaultdict(int)
d = {'parent':3, 'child': 4}
for c in s:
For the example of generating a hashmap to count how many times a letter appears in print(key, value) #each time a key/value pair of d is printed
a string, there is actually another more easy way as below.
#Typical incorrect ways to access key/value
print(key, value)
s = '123ab'
print(key, value)
counter_s = Counter(s)
Useful Data Structures like lists. The differences between tuples and lists are, the tuples cannot be changed unlike
lists and tuples use parentheses, whereas lists use square brackets.
1. Hashmap/Dictionary
In Python 3, a dict actually performs like a hashmap. A dict is composed of multiple Tuple is very handy whenI need to just group a series of objects, for example, I can use
key/value pairs in the format of key:value, a comman(,) is used to separate each pair. a (x,y) tuples to represent a two dimension point while (x,y,z) to a three dimension
The dict can be initilized like point. It looks very logically and elegant.
a={1:2,’strKey’:’strValue’} As mentioned above, a tuple is immutable which means it can not be changed once
defined. Look at below example:
Any hashable type can be key. Please note that list (like [1,2,3,4]) and dict (like {‘1’:2})
are not hashabel.
contains smallest value. So Python’s heapq is actually min heap which means on the
a = (1,0)
>>> h = []
a = [1,0]
>>> heappush(h, (5, 'write code'))
a[0] = 2
>>> heappush(h, (1, 'write spec'))
>>> heappop(h)
Very often, list in Python3 is used as a stack. Data can be pushed to a stack or popped
up. The only magic here is the first pushed data will be the last one to pop up (often A heap queue can be initialized in 2 ways:
referred as “first in last out” FILO). The next to-be-popped data stays at the top of
stack. Normally a peek function is expected to take a look at the data at the top of stack 1. use a empty list (see above example)
without actually popping it up. Look at this example to get more understanding:
2. use heapq.heapify(list)
s = []
>>> heapify(h)
>>> heappop(h)
a = s.pop()
>>> heappop(h)
print(s) # print [1,2,3], peek(unlike pop) will not change a stack Pleaes note that each time heappush/heappop is called (when a new element is added
or removed), the heap is adjusted internally to againt ensure itself a min heap
4, Heap Queue (therefore, each element is equal to or less than its children, and smallest one on the
Please note that this is a summary and extention with example of official document top).
https://docs.python.org/3/library/heapq.html.
There are 2 very useful functions in heapq:
Within Python’s heapq, all elements are placed in a binary tree where any element’s
heapq.nlargest(n, iterable, key=None)
value will be less than or equal to any of its children. Not surprisely, the root of the tree
heapq.nsmallest(n, iterable, key=None)
where key is a function of one argument that is used to extract a comparison key from
head = ListNode()
...
preNode.next =
preNode = curNode
>>> h = [(1,4),(2,3)]
return head.next
>>> heapify(h)
>>> nsmallest(2,a)
To connect this technique to real world examples, I attach 2 practices of it as below.
[(1, 4), (2, 3)]
Major heapq operations (heapify, heappush, heappop) has O(N) time complexity while
some (nsmallest, nlargest) has O(1).
Some Leetcode problems (which aim to find top K smallest/largest elements) can be
easily solved by using heap:
head = None
...
if head == None:
header = ListNode()
else:
preNode.next = ListNode()
preNode = curNode
...
return head
The reason is preNode can be None if the linked list does not yet exist. To make above
code more concise and beautiful, there is a technique called dummy head: I don’t
create the linked list when needed, instead, I will just go ahead and create the head
and have preNode pointing to it so that preNode is always valid. Accordingly, I should
remember the real data starts on head.next instead of head since the head is just a
dummy placeholder. Compare below code snippet with the previous one to get a
understanding of that.