dict.get(key, default=None)
用法如下:https://www.runoob.com/python/att-dictionary-get.html
- key -- 字典中要查找的键。
- default -- 如果指定键的值不存在时,返回该默认值。
如下代码相当于 C++ 的 switch-case 语句,比 if-else 语句简洁很多,易于扩展,且不易出错。
# from https://github.com/DropEdge/DropEdge/blob/master/src/normalization.py
def fetch_normalization(type):
switcher = {
'NormLap': normalized_laplacian, # A' = I - D^-1/2 * A * D^-1/2
'Lap': laplacian, # A' = D - A
'RWalkLap': random_walk_laplacian, # A' = I - D^-1 * A
'FirstOrderGCN': gcn, # A' = I + D^-1/2 * A * D^-1/2
'AugNormAdj': aug_normalized_adjacency, # A' = (D + I)^-1/2 * ( A + I ) * (D + I)^-1/2
'BingGeNormAdj': bingge_norm_adjacency, # A' = I + (D + I)^-1/2 * (A + I) * (D + I)^-1/2
'NormAdj': normalized_adjacency, # D^-1/2 * A * D^-1/2
'RWalk': random_walk, # A' = D^-1*A
'AugRWalk': aug_random_walk, # A' = (D + I)^-1*(A + I)
'NoNorm': no_norm, # A' = A
'INorm': i_norm, # A' = A + I
}
# func = switcher.get(type, lambda: "Invalid normalization technique.")
func = switcher.get(type, lambda x: print("Invalid normalization technique.")) # 似乎不能写 default=lambda...
return func