python学习记录(四)-意想不到
计数
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学习记录(四)-意想不到的更多相关文章
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- python学习第四次笔记
python学习第四次记录 列表list 列表可以存储不同数据类型,而且可以存储大量数据,python的限制是 536870912 个元素,64位python的限制是 1152921504606846 ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
- leveldb 学习记录(四)Log文件
前文记录 leveldb 学习记录(一) skiplistleveldb 学习记录(二) Sliceleveldb 学习记录(三) MemTable 与 Immutable Memtablelevel ...
- Python学习记录day8
目录 Python学习记录day8 1. 静态方法 2. 类方法 3. 属性方法 4. 类的特殊成员方法 4.1 __doc__表示类的描述信息 4.2 __module__ 和 __class__ ...
- Python学习记录day7
目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...
- Python学习(四)数据结构(概要)
Python 数据结构 本章介绍 Python 主要的 built-type(内建数据类型),包括如下: Numeric types int float Text Sequence ...
- JavaScript学习记录四
title: JavaScript学习记录四 toc: true date: 2018-09-16 20:31:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...
- Python学习记录:括号配对检测问题
Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...
随机推荐
- spring security 从零开始搭建
1.引入 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- docker方式安装awvs和nessus渗透工具
docker-compose.yaml文件 version: '2' services: awvsnessus: image: leishianquan/awvs-nessus:v4 environm ...
- css - contenteditable
css - contenteditable contenteditable属性 contenteditable 属性是 HTML5 中的新属性.规定是否可编辑元素的内容. 让contenteditab ...
- JWT & 用户身份认证演变过程
一.起源 0.HTTP无状态 HTTP是无状态的,服务端和客户端如何保持登录状态? 工程师在服务端搞了亿点事情, 就有了下面的解决方案. 1.session认证 (1)什么是session? 服务器为 ...
- react对于setState的写法
react对于setState的写法,改变state的数组里边的值也可以这样写 [1,2,3,4,5].forEach((item)=>{ let arr={} arr[`list${item} ...
- noi 1.5 1 求平均年龄
描述 班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位. 输入 第一行有一个整数n(1<= n <= 100),表示学生的人数.其后n行每行有1个 ...
- CF960G
首先我们考虑$n$的情况,显然以$n$为分界线可以将整个序列分成两部分,就像这样: . 那么我们考虑:在这个东西前面才会有前缀最大的统计,在这个东西后面才会有后缀最大的统计 这样就剩下了$n-1$个元 ...
- Ansible AWX
Ansible简介 ansible是一个非常简单的it自动化平台,使得程序和系统更易于部署.ansible本质是一个进行封装的shell,优点在于它是去中心化的工具,可以直接通过ssh远程管理主机,实 ...
- Vue3引用全局js
在vue3中引入全局js: 1,创建一个js文件: 2,在main.js中引入该js文件: import comm from './utils/comm' app.config.globalPrope ...
- 2.21(html)