首页 > 编程语言 >python Counter

python Counter

时间:2023-02-03 15:36:16浏览次数:55  
标签:python Counter most common L1 print L3

from collections import Counter


L1 = 'iuasdhfiuhaefi'
L2 = [1,2,3,4,5,6,6,6,7,7,8,8,8,11,2,1]
L3 = {1:2,3:4,5:6}
L4 = {1,2,23,4,5,6,76,7}
print(Counter(L1))
print(Counter(L2))
print(Counter(L3)-Counter(L3))
print(Counter(L4))
# Counter({'i': 3, 'u': 2, 'a': 2, 'h': 2, 'f': 2, 's': 1, 'd': 1, 'e': 1})
# Counter({6: 3, 8: 3, 1: 2, 2: 2, 7: 2, 3: 1, 4: 1, 5: 1, 11: 1})
# Counter()
# Counter({1: 1, 2: 1, 4: 1, 5: 1, 6: 1, 7: 1, 76: 1, 23: 1})
print('-----1')
c = Counter(a=3,b=1)
d = Counter(a=1,b=2)
print(c+d)
print(c&d)  # 取最小
print(c|d)  # 取最大
# Counter({'a': 4, 'b': 3})
# Counter({'a': 1, 'b': 1})
# Counter({'a': 3, 'b': 2})
print('-----2')
c = Counter(a=3,b=1,c=2)
print(c.values())   # 取值
# print(c.clear())    # 清空
print(c.items())
print('most_common',c.most_common())
print('most_common',c.most_common()[::-1])
# dict_values([3, 1, 2])
# dict_items([('a', 3), ('b', 1), ('c', 2)])
# most_common [('a', 3), ('c', 2), ('b', 1)]
# most_common [('b', 1), ('c', 2), ('a', 3)]
c = Counter(a=3,b=1,c=0)
for k,v in c.items():
    print(k,v)
print(c)
c+=Counter()
print(c)
print(c+Counter(L1))
c.update(Counter(L1))
print(c)
print(c-Counter(L1))

c = Counter()
# a 3
# b 1
# c 0
# Counter({'a': 3, 'b': 1, 'c': 0})
# Counter({'a': 3, 'b': 1})
# Counter({'a': 5, 'i': 3, 'u': 2, 'h': 2, 'f': 2, 'b': 1, 's': 1, 'd': 1, 'e': 1})
# Counter({'a': 5, 'i': 3, 'u': 2, 'h': 2, 'f': 2, 'b': 1, 's': 1, 'd': 1, 'e': 1})
# Counter({'a': 3, 'b': 1})

标签:python,Counter,most,common,L1,print,L3
From: https://www.cnblogs.com/mktom/p/17089409.html

相关文章

  • 【工具】-Misc-Python-dsstore
    介绍这是一个.DS_Store解析工具。什么是.DS_Store.DS_Store是DesktopServicesStore的缩写,是macOS操作系统上的一个不可见文件,只要您使用“Finder”查看文件夹,它......
  • #Python 文本包含pandas的 Series.str.contains函数
    一:基础的函数组成’’‘Series.str.contains(pat,case=True,flags=0,na=nan,regex=True)’’'测试pattern或regex是否包含在Series或Index的字符串中。返回布尔值系列......
  • Python算术运算符
    Python算术运算符以下假设变量: a=10,b=20:运算符描述实例+加-两个对象相加a+b输出结果30-减-得到负数或是一个数减去另一个数a-b输出结果-10*乘-两个数相乘或......
  • Python中有几种数据类型?
    变量用来存储数据,那么大家有没有想过,我们应该让变量占用多大空间,保存什么样的数据呢?问:说说Python中有几种数据类型?答:Python中主要有8种数据类型:number(数字)、string(字......
  • python基础day03
    #作用:记录/存多个值,可以方便地取出来指定位置的值,比如人的多个爱好,一堆学生姓名#定义:在[]内用逗号分隔开多个任意类型的值l=[10,3.1,'egon',['a','b']]#l=list([10,3.......
  • Micropython 之 旋转立方块
    入门ESP32-C3,学习Micropython还是先从自己手上的模块开始,MPU6050是一个很好的选择,很多场合都用得上。记得几年前用STM32F103接过MPU6050,然后用匿名上位机观测模拟飞机的飞......
  • Python \xee16进制转中文汉字
    在网络传输的时候会默认编码问题再加上编码器老旧的问题导致utf-8反编不成功直接导入到16进制数据变为ascll码试一试正好一下成功代码例子如下 importbinascii ......
  • Python判断字符串是否为空
    Python判断字符串是否为空和null方法实例判断python中的一个字符串是否为空,可以使用如下方法1、使用字符串长度判断len(s)==0则字符串为空#!/user/local/python/bin/......
  • Python fir 单线程下载脚本
    importrequests,os,timeimporturllib3urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)frompathlibimportPathfromtqdmimporttqdmfrom......
  • Python 高级编程之面向对象(一)
    目录一、概述二、面向对象的概念介绍1)类定义2)类属性3)类方法4)构造方法(init)三、面向对象封装、继承和多态1)封装2)继承3)子类重新方法和super()调用父类方法4)多态一、概述科班......