计数

from collections import Counter
# 计数
res = Counter(['a','b','a','c','a','b'])
print(res,type(res))
# Counter({'a': 3, 'b': 2, 'c': 1}) <class 'collections.Counter'>
print(dict(res)) # {'a': 3, 'b': 2, 'c': 1}
# 按次数创建容器
res = Counter(a=3,b=2,c=0)
print(list(res.elements())) # ['a', 'a', 'a', 'b', 'b']
# 最常见排序
res = Counter('abcefcaabf').most_common(3)
print(res,type(res)) # [('a', 3), ('b', 2), ('c', 2)] <class 'list'>

数组中排成最小数(cmp_to_key)

# [3,30,34,59] ==> 3033459
def fun_rules(x,y):
a,b = x + y,y + x
if a>b:
return 1
elif a<b:
return -1
else:
return 0 import functools
varlist = [3,30,34,59]
varlist = [str(i) for i in varlist]
varlist.sort(key=functools.cmp_to_key(fun_rules))
res = ''.join(varlist)
print(res) # 3033459

数组中排成最小数(全排序)

varlist = [3,30,34,59]
from itertools import permutations
res = list(permutations(varlist))
print(res)
varnew = []
for i in res:
temp = [str(x) for x in i]
varnew.append(''.join(temp))
print(varnew)
varnew.sort()
print(varnew[0]) # 3033459

全排列

from itertools import permutations
# 不指定长度
varlist = [1,2,3]
res = permutations(varlist)
print(list(res)) # [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
# 指定长度
res = permutations(varlist,r=2)
print(list(res)) # [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

迪卡尔集

from itertools import product
list1 = ['a','b']
list2 = [1,2]
res = product(list1,list2,repeat=1)
print(list(res)) # [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

排列组合排序

# 不重复
from itertools import combinations
varlist = [2,1,3,4]
res = combinations(varlist,r=2)
print(list(res)) # [(2, 1), (2, 3), (2, 4), (1, 3), (1, 4), (3, 4)]
# 可重复
from itertools import combinations_with_replacement
res = combinations_with_replacement(varlist,r=2)
print(list(res)) # [(2, 2), (2, 1), (2, 3), (2, 4), (1, 1), (1, 3), (1, 4), (3, 3), (3, 4), (4, 4)]

截止到当前位置求和/取最大值

from itertools import accumulate
nums = [1, 3, 2, 4]
print(list(accumulate(nums))) # [1, 4, 6, 10]
print(list(accumulate(nums,max))) # [1, 3, 3, 4]

2个容器取对应值

from itertools import compress
nums1 = ['a','b','c','d']
nums2 = [1, 0, 1, 0]
print(list(compress(nums1, nums2))) # ['a', 'c']

最新文章

  1. ACM集训的第。。。诶~不知道第几题=.=
  2. docker使用中国镜像
  3. [转]解决a different object with the same identifier value was already associated with the session错误
  4. yii2中事务不能回滚的解决
  5. 【python】入门学习(六)
  6. 以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转)
  7. hadoop2.0单机安装
  8. MUI 页面传值 webview
  9. php 共享内存
  10. PostgreSQL的 fdw 跨库使用
  11. 转载:DIV+CSS有可能遇到的问题
  12. 重置kafka的offset
  13. mysql数据库的简单操作
  14. YUV格式总结
  15. 对于百川SDK签名验证的问题
  16. 10招搞定web设计风格指南
  17. jquery获取选中的文本和值
  18. 423 重温Java Script and jQuery 葵花宝典 Bootstrap
  19. kafka工作流程| 命令行操作
  20. Linux(CentOS)下,下载安装Nginx并配置

热门文章

  1. ORihard KCU116E: 经济实惠的 100Gbps 网络和存储 FPGA 开发平台
  2. Linux环境下将.net6项目部署到docker
  3. python web学习
  4. js循环中删除数组中的某个元素
  5. Cloudflare.com设置域名URL转发
  6. spider_使用urllib库 提交post请求,有道翻译案例
  7. nchu第二次面向对象编程博客作业
  8. ios怎么在自己公司网站扫描下载安装IPA安装包
  9. Leetcode本地阅读器开发--01界面设计一
  10. 在Unity3D中开发的Ghost Shader