List
- l.pop(i)弹出第i个元素
- for index, num in enumerate(nums)
- l[::-1]
- l.count(x)
list comprehension:
newlist = [x if x != "banana" else "orange" for x in fruits]
Sort 1:
min(l, key = lambda x: abs(target - x))
Sort 2:
Sort Array by Increasing Frequency:def frequencySort(self, A): count = collections.Counter(A) return sorted(A, key=lambda x: (count[x], -x))
Sort 3:
sentences is a list of sentences, times is a list of numbers indicating how many times that sentence appears. Sort sentences by times and then alphabet.# https://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list # https://stackoverflow.com/questions/16193578/how-do-i-perform-secondary-sorting-in-python # https://stackoverflow.com/questions/4659524/how-to-sort-a-list-by-length-of-string-followed-by-alphabetical-order self.history=[x for _,x in sorted(zip(times, sentences), key=lambda pair: (-pair[0],pair[1]))] or self.times_dict = dict(zip(sentences, times)) self.history=sorted(sentences, key=lambda x: (-self.times_dict[x],x))
Dictionary
import collection, collections.defaultdict(int), collections.defaultdict(list)
def constant_factory(value): return lambda: value d = defaultdict(constant_factory('<missing>'))
- import collection, counts = collections.Counter(nums)
- dictionary.get(keyname, optional_value_if_key_not_exist_default_None)
- dict(zip(sentences, times))
- Dict in dict: FieldGoalsPerGame[Pdict['KobeBrayant']][Sdict['2009']]
Set
- set1 & set2
Linked List
- head.next
Tree
- if node
- node.val, node.left, node.right
- BST Binary Search Tree: 左<root<右
Create new:
root = Node(2) root.left = Node(1) root.right = Node(3)
Stack
- Min Stack
Heap
- 堆是一种基本的数据结构,堆的结构是一棵完全二叉树,并且满足堆积的性质:每个节点(叶节点除外)的值都大于等于(或都小于等于)它的子节点。
- import heapq
OS Get files
os.getcwd()
Windows: os.chdir('C:\Users\Kirill\Desktop)
Mac: os.chdir('/Users/Kirill/Desktop)
Numpy
np.reshape(data, (5,4), order='C')
np.reshape(data, (5,4), order='F')
OOP Concept:
data.reshape((5,4))
FieldGoalsPerGame = np.matrix.round(FieldGoals/Games)
Will have warnings for divided by 0
import warnings
warinings.filterwarnings('ignore')
Pandas
stats.info(): information of all columns
stats.describe().transpose()
Filter=stats.InternetUsers<2
Filter2=stats.BirthRate>40
stats[Filter]
Filter and Filter2 : error
Filter & Filter2: works
stats[Filter & Filter2]
stats.iat[0,1]: for integer location
stats.at[0,'BirthRate']: at for labels
Plot
plt.plot(Salary[0], c='Black', ls='--', market='s', ms=7, label=Players[0])
plt.plot(Salary[1], c='Red', ls='--', market='o', ms=7, label=Players[1])
plt.plot(loc='upper left', bbox_to_anchor=(1,1))
plt.xticks(list(range(0,10)), Seasons, rotation='vertical')
plt.show()
Class
Template:
class AutocompleteSystem: def __init__(self, a: List[str], b: List[int]): self.a=a ... def input(self, c: str) -> List[str]: ... return d
SQL
%macro Pulldata(mth, dte);
Proc SQL;
CONNECT using db2;
CREATE TABLE FULL_XAA_&mth._1 AS SELECT * FROM CONNECTION TO db2
(SELECT * FROM DW.RLTN_DIM)
其他
- float('inf')
- mod(value,2)
- ord('A')
- yield
常用解题技巧
- 遍历过程中用hashmap存储,避免重复遍历
- 2 pointers
- 二分法,3 pointers(left,pivot,right)
- Dynamic Programming, 存储上一轮的结果,遍历过程中不断更新变量
- DFS Depth First Search
- BFS Breadth First Search
- bucket sort / counting sort
- Greedy: 贪心算法(greedy algorithm),又称贪婪算法,是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是最好或最优的算法。贪心算法与动态规划的不同在于它对每个子问题的解决方案都做出选择,不能回退。动态规划则会保存以前的运算结果,并根据以前的结果对当前进行选择,有回退功能。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。