1 # list元素求和 2 sum = reduce(lambda x,y: x+y, mylist)
1 # 比较两个 lists 的元素是否完全一致 2 if all(x==y for x, y in zip(X, Y)) 3 do something
1 # 按照 key 排列字典的元素,以libsvm格式输出 2 sorted(dd.keys()) 3 sorted_dict = [(key, dd[key]) for key in sorted(dd.keys())] 4 # 以libsvm格式打印
5 print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd.keys())])
1 # 按照 value 排序字典的元素 2 # 仅输出排序后的key 3 sorted_key = [key for key in sorted(dd, key=dd.get, reverse=True)] 4 # 仅输出排序后的字典 5 sorted_dict = [(key, dd[key]) for key in sorted(dd, key=dd.get, reverse=True)] 6 # 以libsvm格式打印字典 7 print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd, key=dd.get, reverse=True)])
1 # get key intersection of two dictionaries 2 intersect = set(dict_A.keys()) & set(dict_B.keys())
1 # sort a list of tuple 2 # 以tuple的第零个元素排序 3 sorted_list = sorted(tuple_list, key=lambda x:x[0])
1 # 把libsvm格式数据转换为dict对象 2 mydict = {x.split(':')[0]:x.split(':')[1] for x in mylist}
1 # sort list and return index of list 2 # 对list排序,返回有序元素的原始下标值 3 sorted_index = sorted(range(len(list)), key=lambda k: list[k])