python函数enumerate,operator和Counter使用技巧实例小结

作者:Nicoder 时间:2022-08-09 07:02:32 

本文实例讲述了python函数enumerate,operator和Counter使用技巧。分享给大家供大家参考,具体如下:

最近看人家的代码,发现了很多python内置的函数和数据结构,可以大大减少我们的代码量(而且人家是优化过的)。python以简洁和强大的库著称,要是什么都自己写的话,太不python了。这里介绍常用的能大大提高我们生活幸福度的小技巧:

enumerate()

这个函数主要用于既要遍历元素又要记下索引。通常我们都会这样写(不能再笨拙了):


for i in range(0,len(list)):
 print i,list[i]

使用enumerate:


for index,text in enumerate(list):
 print index,text

itemgetter()

这个和下面的函数在operator库中,通常用在排序中。例如要对一个tuple的列表进行排序,找到第二个值最小的那个tuple。我一开始这样做:


list_of_tuples = [(1,2), (3,4), (5,0)]
min_tuple = None
minimum = sys.maxint
for pair in list_of_tuples:
 x,y = pair
 if y < minimum:
   min_tuple = pair
print min_tuple

后来,进化了,这样做:


def snd(pair):
 x,y = pair
 return y
list_of_tuples = [(1,2), (3,4), (5,0)]
min(list_of_tuples, key=snd)

这样看起来好多了,可是我发现人家的代码里是这样做的:


import operator
list_of_tuples = [(1,2), (3,4), (5,0)]
min(list_of_tuples, key=operator.itemgetter(1)) #use 2nd value

attgetter()

这个函数和上面的函数很像,不同的是,上面是用索引获取对应值,而这里使用属性来获取对应值,就像dict一样。

假如我们有个类Student:


class Student(object):
 def __init__(self, id, name, marks):
   self.id = id
     self.name = name
     self.marks = marks
 def __str__(self):
   return '%s has marks %s' %(self.name, self.marks)

我们有一个学生的实例的列表students,需要从里面找到分最高的。最简介的做法是:


students = [ Student(0, 'Foo', 30), Student(1, 'Bar', 95), Student(2, 'Baz', 80)]
best_student = max(students, key=operator.attrgetter('marks')) # don't forget the quotes

collections.Counter()

一看这个函数名,就知道是用来统计个数的~返回的是一个dict,key是各个元素,value是对应的个数。一开始,我并不知道这个函数,都是这样统计的:


d={}
for text in list:
 if not text in d.keys():
   d[text]=1
 else:
   d[text]+=1
print d

现在,直接一句话:


counter=collections.Counter(list)
print counter

之后获取各元素的个数,使用方法同一般的dict。如果增加元素e1,e2和对应个数,直接counter.update({e1=4,e2=3})就可以。

此外,这个Counter不仅用来数数,还有好多函数方便各种操作,例如:

  • counter.most_common(n) 返回数目最多的前n个元素和对应个数

  • a.substract(b) 返回一个Counter,Counter a减去Counter b,多的元素个数为正值,少的元素个数为负值

  • counter.elements() 返回一个element列表,其中每个元素有多少个就重复多少次

  • counter.values() 返回个数列表,通常配合sum(counter.values())

  • counter.clear() 重置counter

  • del counter[e]删除元素e和它的纪录

  • 算数操作:+和- ,对应元素个数加减;|和&,两个counter的并集和交集

关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.csdn.net/hejunqing14/article/details/52517367

标签:python,enumerate,operator,Counter
0
投稿

猜你喜欢

  • 面试官常问之说说js中var、let、const的区别

    2024-05-09 15:06:58
  • Python利用DNN实现宝石识别

    2023-08-07 05:49:18
  • Python中元组的概念及应用小结

    2022-03-28 15:01:05
  • 在Python的Flask框架中使用日期和时间的教程

    2022-08-14 21:13:09
  • .img/.hdr格式转.nii格式的操作

    2023-08-25 04:56:14
  • Bootstrapvalidator校验、校验清除重置的实现代码(推荐)

    2024-04-10 13:52:57
  • Python 中 f-Strings 的作用

    2022-12-04 11:44:55
  • Three.js利用orbit controls插件(轨道控制)控制模型交互动作详解

    2024-05-22 10:31:00
  • asp如何用HtmlEncode来显示Unicode编码?

    2010-06-12 12:49:00
  • Go语言中普通函数与方法的区别分析

    2024-02-08 18:02:23
  • 新手必备Python开发环境搭建教程

    2023-07-09 03:25:35
  • 让你一文弄懂Pandas文本数据处理

    2023-07-17 19:12:08
  • Python实现信息管理系统

    2022-10-08 22:28:39
  • keras打印loss对权重的导数方式

    2023-05-17 18:21:11
  • 将django项目部署到centos的踩坑实战

    2021-05-14 06:00:22
  • TensorFlow 显存使用机制详解

    2022-02-07 11:46:53
  • python:pandas合并csv文件的方法(图书数据集成)

    2021-06-22 09:34:40
  • PHP控制反转(IOC)和依赖注入(DI)

    2024-05-11 10:09:31
  • Pygame改编飞机大战制作兔子接月饼游戏

    2023-04-09 02:57:22
  • Django-Rest-Framework 权限管理源码浅析(小结)

    2021-11-02 06:18:49
  • asp之家 网络编程 m.aspxhome.com