计数

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. C++之split字符串分割

    在C++中没有直接对应的split函数,字符串分割可借助以下方法实现: 1.借助strtok函数 函数原型:char * strtok (char *str, char * delim); 函数功能: ...

  2. c++dump

    //Minidump.h #pragma once class CMinidump { public: CMinidump(); ~CMinidump(); static void CreateDum ...

  3. 访问修饰符 protected(s)

    protected 受保护的:可以在当前类的内部以及该类的子类中可以访问. using System; using System.Collections.Generic; using System.L ...

  4. 基础篇之DOS命令

    对于编程的小朋友来说,在DOS中输入一些命令也是常有的事情. 今天就来学习一些常见的命令.[该篇是在B站学习Siki学院的课程时做的笔记] DOS命令目录操作 常用DOS命令(输入命令后按下回车) d ...

  5. iOS 扩展与分类的区别

    1.分类 category 分类的作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法 分类也能使用@property 添加属性 [通过runtime 关联对 ...

  6. [mysql练习]多行结果合并问题练习

    有一个scores表,表结构和数据如下: id, stu_id, name,course, grade 1,1,贾万, 语文, 902,1,贾万 ,数学 ,100 3,2,毛之远 ,语文 ,974,2 ...

  7. 将字符串数组String[]转换成List的三种方法

    通过 Arrays.asList(strArray) 方式,将数组转换List后,不能对List增删,只能查改,否则抛异常. String[] strArray = new String[2]; Li ...

  8. 037_Clone Button

    ResolutionTo do this first go to Setup | Customize | Accounts | Buttons and Links | New. Enter the f ...

  9. 【Nday】Spring-Cloud-SpEL-表达式注入漏洞复现

    # 环境搭建 JDK 15下载:   https://www.oracle.com/java/technologies/javase/jdk15-archive-downloads.html 在Cen ...

  10. [JSOI2014]宅男计划

    Description: 外卖店一共有N种食物,分别有1到N编号.第i种食物有固定的价钱Pi和保质期Si.第i种食物会在Si天后过期.JYY是不会吃过期食物的. 比如JYY如果今天点了一份保质期为1天 ...