Common Python Operations
intervals.sort(key=lambda p: p[0])
LC 189 [:] slicing operation
dict
| |
LC 49
defaultdict
defaultdict(int) is more suitable for general scenarios where a default value is needed when a key does not exist, not necessarily just for counting.
| |
Counter
Counter is more suitable for frequency statistics and occurrence analysis of iterables. Its built-in counting methods can simplify many scenarios. Counter(p) means creating a counter from an iterable.
Iterable: list, tuple, str, dict, set, range, enumerate, map
del cnt[x] deletes the count for x. len(cnt) gets the number of elements counted.
Create a new Counter
| |
list
index()
| |
sort()
It sorts the list in place and does not create a new list. It can only be applied to lists.
String comparison
Independent of length
| |
iterable
Iterable: list, tuple, str, dict, set, range, enumerate, map sorted(iterable) returns a new sorted list. The original iterable is not modified. Time complexity is O(nlogn)
deque = collections.deque()
ACM Mode
zip
| |
Output: [(‘Alice’, 90), (‘Bob’, 85), (‘Charlie’, 88)]
heapq
| |