Python数据挖掘与实践笔记(一)

mac2022-06-30  24

Python数据挖掘与实践-1

defaultdict(int)

传递进来的类型参数,不是用来约束值的类型,更不是约束键的类型,而是实现一种值的初始化,如果未对该键赋值的话。所以,defaultdict 的真正意义实现一种全局的初始化,访问任何键都不会抛 KeyError 的异常; defaultdict(int):初始化为 0; defaultdict(float):初始化为 0.0; defaultdict(str):初始化为 ‘’; defaultdict(list):初始化为[]

print 与 pprint 区别

数据来源:chapter 1

pprint.pprint(object,stream=None,indent=1, width=80, depth=None) 输出格式的对象字符串到指定的stream,最后以换行符结束。 -from pprint import pprint pprint(list(support.items())) VS print(list(support.items()))

pprint(list(support.items())) [((2, 3), 25), ((2, 4), 27), ((3, 2), 25), ((3, 4), 21), ((4, 2), 27), ((4, 3), 21), ((0, 1), 14), ((0, 3), 5), ((1, 0), 14), ((1, 3), 9), ((3, 0), 5), ((3, 1), 9), ((0, 2), 4), ((2, 0), 4), ((1, 4), 19), ((4, 1), 19), ((0, 4), 17), ((4, 0), 17), ((1, 2), 7), ((2, 1), 7)];

print(list(support.items()))

[((2, 3), 25), ((2, 4), 27), ((3, 2), 25), ((3, 4), 21), ((4, 2), 27), ((4, 3), 21), ((0, 1), 14), ((0, 3), 5), ((1, 0), 14), ((1, 3), 9), ((3, 0), 5), ((3, 1), 9), ((0, 2), 4), ((2, 0), 4), ((1, 4), 19), ((4, 1), 19), ((0, 4), 17), ((4, 0), 17), ((1, 2), 7), ((2, 1), 7)]`

itemgetter()

from operator import itemgetter sorted_support = sorted(support.items(), key=itemgetter(1), reverse = True)
最新回复(0)