计数

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. git diff如何确定差异所在函数context

    问题 在使用git diff 展示c/c++文件修改内容时,除了显示修改上下文外,输出还贴心的展示了修改所在的函数.尽管这个展示并不总是准确,但是能够做到大部分情况下准确也已经相当不错:是不是git内 ...

  2. DIV CSS遮罩层(弹窗窗口)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 两个DIV的切换

    <html><head><title>HTML元素的显示与隐藏控制</title><script type="text/javascri ...

  4. Activiti5.22.0扩展支持达梦数据库

    前言 目前国内部分公司做的项目要求去ioe使用国产的替代方案.数据从异构数据库中迁移到达梦,应用中使用Activiti工作流在替换了数据库驱动后启动过程报错如下: nested exception i ...

  5. Pytorch Dropout函数

    Dropout layers class torch.nn.Dropout(p=0.5, inplace=False) 随机将输入张量中部分元素设置为0.对于每次前向调用,被置0的元素都是随机的. 参 ...

  6. linux 部署python 系统服务管理命令 yum源设置 linux定时任务 python在linux的虚拟环境安装以及使用

    安装python3 三种方式 ==linux下很多脚本默认都用python2, 所以不要把python3的执行文件改为python,因为linux里默认python就是运行python2版本 == y ...

  7. Unity 复杂Json 解析

    最近项目需要, 搞了一串嵌套非常多的Json 用LitJson读取体验不太好,上网查了一下发现NetwtonsoftJson ,十分好用 一句代码就可以了 JObject data = JsonCon ...

  8. 苹果电脑快捷键使用 Mac快捷键大全详细介绍

    相信有很多刚开始使用苹果电脑操作系统的用户,都并不了解整个MAC操作系统,就连一般快捷键的使用都不清楚,这让人很感觉很苦恼.分享一下Mac快捷键大全的详细介绍,记住这些常用的快捷键,是你熟悉苹果电脑的 ...

  9. Excel—时间函数

    时间函数,学习之前先普及一个尝试,微软采用的是1900年纪念方式(数字1,代表的是1900年1月1日,数字1.5  代表的是1900年的1月1日的中午12:00,因为1.5正好是1天半的时间,而一天2 ...

  10. linux三权分立账号创建

    系统管理员 创建系统管理员(sys用户)并设置密码 [root@localhost ~]# useradd sys [root@localhost ~]# passwd sys 创建组并将用户添加到组 ...