当前位置: 首页>>代码示例>>Python>>正文


Python collections.Counter类代码示例

本文整理汇总了Python中collections.Counter的典型用法代码示例。如果您正苦于以下问题:Python Counter类的具体用法?Python Counter怎么用?Python Counter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Counter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: update

    def update(self, *args, **kwargs):
        """D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
        If E present, does:  for k in E: D[k] = E[k]
        This is followed by: for k in F: D[k] = F[k]

        **Example:**

        >>> e = Exponents()
        >>> e.update(a=1)
        >>> e.update(dict(b=2), c=3)
        >>> e.update('1/d')
        >>> e # doctest: +SKIP
        Exponents({'a': 1, 'b': 2, 'c': 3, 'd': -1})

        .. testcleanup::
           >>> assert e == dict(a=1, b=2, c=3, d=-1)
        """
        try:
            # Assume args[0] is a string.
            arg = args[0].replace(' ', '')  # Remove spaces.
        except (IndexError, AttributeError):
            Counter.update(self, *args, **kwargs)
        else:
            if len(args) > 1:
                raise TypeError("update expected at most 1 arguments, got %i"
                                % len(args))
            Counter.update(self, Exponents.fromstr(arg), **kwargs)
开发者ID:joewa,项目名称:natu,代码行数:27,代码来源:exponents.py

示例2: __setitem__

    def __setitem__(self, item, val):
        if len(self) > self._max * 2:
            print("Purging")
            to_delete = self.most_common()[self._max:]
            for ii in to_delete:
                del self[ii]

        Counter.__setitem__(self, item, val)
开发者ID:jankim,项目名称:qb,代码行数:8,代码来源:inspect_features.py

示例3: __init__

 def __init__(self, _dict=None, **kwargs):
     if _dict is None:
         self.__init__(kwargs)
     else:
         if not all(isinstance(x, Rational) for x in _dict.values()):
             raise TypeError("powers of dimensions must be rational")
         Counter.__init__(self, _dict)
         self.clean()
开发者ID:speezepearson,项目名称:units,代码行数:8,代码来源:dimensions.py

示例4: __init__

 def __init__(self, n, *args, **kwargs):
     assert n>=1
     self._size = n
     self._worst = None
     Counter.__init__(self, *args, **kwargs)
     if len(self)>=n:
         self._worst = self.most_common(n)[-1]
         for k,v in self.most_common()[n:]:
             del self[k]
开发者ID:chenguandan,项目名称:pysupersensetagger,代码行数:9,代码来源:beam.py

示例5: __setitem__

 def __setitem__(self, k, v):
     if k in self:
         assert self[k]==v
         return
     if self._worst is not None: # the beam is full
         if v<=self._worst[1]:
             return
         else:
             del self[self._worst[0]]
             
     Counter.__setitem__(self, k, v)
     assert self._size>=len(self)
     if self._size==len(self):
         self._worst = min(self.items(), key=lambda (k1,v1): v1)
开发者ID:chenguandan,项目名称:pysupersensetagger,代码行数:14,代码来源:beam.py

示例6: addFakeData

def addFakeData(oData,oLabels,count=100,low=10):
	data = oData[:]
	labels = oLabels[:]
	for iafsa in range(count):
		c = Counter(chain(*labels))
		lc = Counter.most_common(c)
	
		dlc = {}
		for l in lc: dlc[l[0]] = l[1]
	
		#teze = [sum([ dlc[y]**2 for y in x])  for x in labels]
		teze = [sum([ dlc[y] for y in x])  for x in labels]
		teze = sorted([(y,x) for x,y in enumerate(teze)])
		tt = teze[:max(low*10,200)]
		shuffle(tt)
		duplicate = [x[1] for x in tt[:low]]
		dLabels = [labels[i][:] for i in duplicate]
		dData = [data[i][:] for i in duplicate]
		for ii in range(1):
			for i in range(len(duplicate)):
				labels.append(dLabels[i])
				data.append(dData[i])
	#shuflamo vrstice da niso vec lepo, pa poskrbimo da labele ostanejo 
	#pri svojem primeru
	sd = []
	[sd.append((data[i],labels[i])) for i in xrange(len(data))]
	shuffle(sd)
	ll = []
	dd = []
	for x,y in sd:
		dd.append(x)
		ll.append(y)
	return (dd, ll)
开发者ID:zidarsk8,项目名称:dataMining,代码行数:33,代码来源:data.py

示例7: wanabeknn

def wanabeknn(k=15):
	from collections import Counter
	ftrd = open("minidata/trainingData.csv")
	fted = open("minidata/testData.csv")
	flab = open("minidata/trainingLabels.csv")

	lab = [[int(j) for j in i.strip().split(",")]  for i in flab.readlines()]
	trd = [[int(j) for j in i.strip().split("\t")] for i in ftrd.readlines()]
	ted = [[int(j) for j in i.strip().split("\t")] for i in fted.readlines()]

	def dist(a,b): return sum([min(a[i], b[i]) for i in xrange(len(a))])

	rez = []
	for v in ted:
		print "hurej  %4d   %3d" % ( len(rez),len(rez[-1:]))
		t = []
		for trindex, train in enumerate(trd):
			t.append((dist(train, v), trindex))
		tt = sorted(t, reverse=True)
		ll = []
		for i in range(k): ll += lab[tt[i][1]]
		n = len(ll)
		for i in range(k/3): ll += lab[tt[i][1]]
		rez.append([x[0] for x in Counter.most_common(Counter(ll),n/k)])
		print rez
	cPickle.dump(rez, file("rezPickled/wnbknn%d.pickled" % k, "w"), -1)
开发者ID:eagle12td,项目名称:dataMining,代码行数:26,代码来源:main.py

示例8: __init__

    def __init__(self, max_len, N_max = 5):
        '''
        @summary: NGramSet Constructor

        @param max_len: 最优序列长度
        @param N_max: CCS2012 对应最大n-gram

        '''
        Counter.__init__(self)
        
        self.N_max = N_max
        self.max_len = max_len
        
        self.alphabet_size = 0    # 记录项总数
        self.all_record_num = 0   # 记录数据集中记录总数
        self.TERM = 0             # 序列结束符
开发者ID:luguoqing,项目名称:Diff-FSPM,代码行数:16,代码来源:NGramSet.py

示例9: _most_preferred

 def _most_preferred(self, alternatives):
     """Applies funcnamei from each trait to the alternatives and return the most preferred."""
     prefs = [y for y in [getattr(x, funcnamei)(alternatives) for x in self.traits] if y is not None]
     if not prefs:
         return None
     if len(prefs) == 1:
         return prefs[0]
     return Counter.most_common(Counter(prefs), 1)[0][0]
开发者ID:Mitten-O,项目名称:freeorion,代码行数:8,代码来源:character_module.py

示例10: items

 def items(self, relative=False):
     """ Returns a list of (key, value)-tuples sorted by value, highest-first.
         With relative=True, the sum of values is 1.0.
     """
     a = Counter.most_common(self)
     if relative:
         n = sum(v for k, v in a) or 1.
         a = [(k, v / n) for v, k in a]
     return a
开发者ID:markus-beuckelmann,项目名称:pattern,代码行数:9,代码来源:metrics.py

示例11: __len__

 def __len__(self):
     '''Returns the total number of members, excluding repeated elements.
     
     >>> m = multiset('abb')
     >>> len(m)
     2
     
     '''
     return Counter.__len__(self)
开发者ID:jvb,项目名称:infobiotics-dashboard,代码行数:9,代码来源:multiset.py

示例12: most_common_viz

def most_common_viz(output_dir: str, ints: collections.Counter) -> None:
    df = pd.DataFrame(ints.most_common(), columns=["Integer", "Frequency"])

    with open(os.path.join(output_dir, 'index.html'), 'w') as fh:
        fh.write('<html><body>\n')
        fh.write('<h3>Most common integers:</h3>\n')
        fh.write(df.to_html(index=False))
        fh.write('</body></html>')

    with open(os.path.join(output_dir, 'index.tsv'), 'w') as fh:
        fh.write(df.to_csv(sep='\t', index=False))
开发者ID:jairideout,项目名称:qiime2,代码行数:11,代码来源:visualizer.py

示例13: __add__

    def __add__(self, other):
        """Add together, with recursion.  The basic idea is that the set of keys
        should be added together and then recurse the addition to the values where keys are shared
        otherwise just add the value.

        >>> m, m2 = Mdict('a'), Mdict('abb')
        >>> m + m2 == {'a':2, 'b':2}
        True
        >>> m + 1 == {None:1, 'a':1}
        True
        >>> m['a'] = m2  #now fractal
        >>> m == {'a': {'a':1, 'b':2}}
        True
        >>> m + m + 1 == {'a': {'a':2, 'b':4}, None: 1}
        True
        """
        try: # need to add (union) of keys, and then recurse into values of common keys
            return Mdict(Counter.__add__(self, other)) #Counter.__add__(self, other)
        except TypeError: #must've been an integer for other
            return Mdict(Counter.__add__(self, Counter({None: other})))
开发者ID:theProphet,项目名称:Pangaia,代码行数:20,代码来源:metadict.py

示例14: removeLeastCommonData

def removeLeastCommonData(oData, oLabels, least=5):
	data = oData[:]
	labels = oLabels[:]
	c = Counter(chain(*labels))
	lc = Counter.most_common(c)
	bb = sorted(list(Set([j for i,j in lc])))
	a = [x[0] for x in lc if x[1] < bb[5]]
	rem = [i for i,j in enumerate(labels) if len(Set(j).intersection(Set(a))) > 0 ]
	[labels.pop(x) for x in sorted(rem, reverse=True)]
	[data.pop(x) for x in sorted(rem, reverse=True)]
	return (data, labels)
开发者ID:zidarsk8,项目名称:dataMining,代码行数:11,代码来源:data.py

示例15: removeMostCommonData

def removeMostCommonData(oData, oLabels, count=20):
	data = oData[:]
	labels = oLabels[:]
	for iafsa in range(count):
		c = Counter(chain(*labels))
		lc = Counter.most_common(c)
		dlc = {}
		for l in lc: dlc[l[0]] = l[1]
		teze = [max([ dlc[y] for y in x])  for x in labels]
		teze = sorted([(y,x) for x,y in enumerate(teze)])
		rem = [x[1] for x in teze[-10:]]
		[labels.pop(x) for x in sorted(rem, reverse=True)]
		[data.pop(x) for x in sorted(rem, reverse=True)]
	return (data, labels)
开发者ID:zidarsk8,项目名称:dataMining,代码行数:14,代码来源:data.py


注:本文中的collections.Counter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。