计数

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']

python学习记录(四)-意想不到的更多相关文章

  1. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  2. python学习第四次笔记

    python学习第四次记录 列表list 列表可以存储不同数据类型,而且可以存储大量数据,python的限制是 536870912 个元素,64位python的限制是 1152921504606846 ...

  3. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  4. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  5. leveldb 学习记录(四)Log文件

    前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...

  6. Python学习记录day8

    目录 Python学习记录day8 1. 静态方法 2. 类方法 3. 属性方法 4. 类的特殊成员方法 4.1 __doc__表示类的描述信息 4.2 __module__ 和 __class__ ...

  7. Python学习记录day7

    目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...

  8. Python学习(四)数据结构(概要)

    Python 数据结构 本章介绍 Python 主要的 built-type(内建数据类型),包括如下: Numeric types          int float Text Sequence ...

  9. JavaScript学习记录四

    title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  10. Python学习记录:括号配对检测问题

    Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...

随机推荐

  1. Linux7重置root管理员密码

    1.先确定是否为rhel 7系统(别的系统我没有试过,请慎重尝试) cat /etc/redhat-release 查看Linux的操作系统 2.按键盘e键进入内核编辑界面 3.找到linux16这行 ...

  2. 043_关于Salesforce集中权限的解释

    1.创建Object的时候,一定要选中Deploy,避免在All Tabs 中找不到 2.在Profile里,选择 Standart tab Setting.Custom tab setting,有三 ...

  3. centos-7部署kafka-v2.13.3.0.1集群

    1.部署测试机器规划 ip         kafka 版本   zookeeper 版本 192.168.113.132        v2.13.3.0.1 v3.6.3 192.168.113. ...

  4. Java-token生成

    1. 引入jar包 <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt< ...

  5. ??????FormDataAccess

    using Genersoft.Fms.Center.Pub.Com;using Genersoft.Platform.AdpBizIntegration.Entity;using Genersoft ...

  6. 使用IScroll组件出现滑动卡顿问题解决方法

    var is = new IScroll('#wrapper2', { scrollX: true, scrollY: false, click: true, keyBindings: true, / ...

  7. hdu4585 Treap与名次树/STL map(C/C++)

    hdu4585 题目地址:https://acm.dingbacode.com/showproblem.php?pid=4585 Shaolin Time Limit: 3000/1000 MS (J ...

  8. uniapp项目 hbuilder工程转cli工程 hbuilder工程不可以用命令行打包

    hbuilder工程不可以用命令行打包,只能用自带的发行手动打包 cli工程可以用命令行打包,可以配置多环境 1.安装空的cli项目 vue create -p dcloudio/uni-preset ...

  9. deepin 调整微信、百度网盘、迅雷等等软件字体的方法

    一.修改微信字体大小方法: 1.方法一:修改deepinwine桌面环境字体 env WINEPREFIX="$HOME/.deepinwine/Deepin-WeChat" wi ...

  10. VS2019创建WebAPI,本地发布WebAPI

    一.创建WebAPI 1.打开VS2019->创建新项目 2.ASP.NET Web应用程序->下一步 3.注意:.NET Framework版本必须高于4.0以上 4.选择"W ...