Table of Contents 1. class collections.Counter([iterable-or-mapping]) 1.1. 例子 1.2. 使用实例 2. To Be Continued class collections.Counter([iterable-or-mapping]) Counter 是实现的 dict 的一个子类,可以用来方便地计数. 例子 举个计数的例子,需要统计一个文件中,每个单词出现的次数.实现方法如下 # 普通青年 d = {} with op
getsizeof的局限 python非内置数据类型的对象无法用sys.getsizeof()获得真实的大小,例: import networkx as nx import sys G = nx.Graph() l = [i for i in xrange(10000)] print "size of l:", sys.getsizeof(l) G.add_nodes_from(l) print "size of graph:", sys.getsizeof(G)
Counter class https://docs.python.org/2/library/collections.html#collections.Counter # class collections.Counter([iterable-or-mapping]) # A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are sto
python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成完全随机的整数,用numpy中的random.randint index = np.random.randint(0,10,size=10) 生成的是可能会重复的10个从0~10的整数
刚开始学python时候,发现一个很迷惑的现象,一直到看了源码后才知道了: >>> a=6 >>> b=6 >>> a is b True 想用同样的参数初始化两个对象,结果却是,这两个对象其实是同样的对象????逗我呢? >>> a=666 >>> b=666 >>> a is b False 这又是怎么回事?为什么现在又是False了??? 这些不同,主要来自于python中对待小整数和大整数的
/********************************************************************* * Author : Samson * Date : 09/19/2014 * Test platform: * Linux ubuntu 3.2.0-58-generic-pae * GNU bash, version 4.2.39 * ***********************