python trick 2018-11-04 Python Python Some elegant elements of writing python. reference Tricks for python transpose 12345mat = [[1,2,3],[4,5,6]]transpose = zip(*mat) # unpack matprint(list(transpose))# [[1,4], [2,5], [3,6]] chained function call 123456789def product(a,b): return a * bdef add(a,b): return a + bflag = Trueprint((product if flag else add)(5,7)) sort Dictionary by Value 123456789101112131415dic = { "a":132 "b":214 "d":44}# 1sorted(dic.items(), key=lambda x: x[1])# 2from operator import itemgettersorted(dic.items(), key=itemgetter(1))# 3sorted(dic, key=dic.get) merget dict 12345678d1 = {1:2}d2 = {2:3}# just for python 3.5# 1 {**d1, **d2}dict(d1.items() | d2.items())d1.update(d2) Older CMU 11-731(MT&Seq2Seq) Advanced Topics: Adaptation Methods