import re
re.sub(r'\W+', '', string)- Sub all non-alphanumeric elements
\Wwith''
import statistics
from statistics import mode
List = [2, 1, 2, 2, 1, 3]
print(mode(List))
# 2 list(string)a = [1, 3, 5]
b = [2, 4, 6]
for num1, num2 in zip(a, b):
print(num1),
print(num2)
# 1 2 3 4 5 6- Add new elements to the top of the stack
- Remove elements in LIFO order
- Speed issues once large
listsare stored to provide fast access to random elements- Elements stored next to each other (Python knows exactly where to look in memory to find it)
- Amortized constant time - if the stack grows bigger than the block of memory, Python does memory allocations
From collections import deque
theStack = deque()- Implemented with Double Linked Lists
- Constant time addition + removal of entries
- Random indexing is slower, but that is not what we use a stack for
q = deque()
q.append(1)
q.append(2)
q.append(3)
q[0] # 1
q[-1] # 3- Just like an array
a[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole arraya[start:stop:step] # start through not past stop, by step
a[slice(start, stop, step)] # equivalent slice code:stopis not inclusive!
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two itemsa[::-1] # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed- These values can also be negative
arr[i], arr[j] = arr[j], arr[i]- Assigns
arr[i] = arr[j]andarr[j] = arr[i]all in 1 go - The comma forms a tuple
Destructuring with lists/tuples:
a, b = [1, 2] # a = 1, b = 2
a, b = (1, 2) # a = 1, b = 2 (comma operator above)
def fx():
return (1, 2, 3) # () optional, just add readability for tuples
a, b, c = fx() # a = 1; b = 2; c = 3