1、如果想得到一个列表的index和内容,可以通过enumerate快速实现

drinks = ['coffee','tea', 'milk', 'water']
for index, drink in enumerate(drinks):
print ('Item {} is {}'.format(index, drink))
#Result
# Item 0 is coffee
# Item 1 is tea
# Item 2 is milk
# Item 3 is water

2、Python 中的set, 是一个无序不重复元素集,可以非常方便的进行关系测试和消除重复元素

# deduplicate a list fast
print (set(['ham', 'eggs','bacon','ham']))
# Result
# {'ham', 'eggs', 'bacon'}
# compare list to find difference/similarities
# {} without "key":"value" pairs makes a set
menu = {'pancakes', 'ham', 'eggs', 'bacon'}
new_menu = {'coffee', 'ham', 'eggs', 'bagels', 'bacon'} new_items = new_menu.difference(menu)
print ('try our new', ', '.join(new_items)) # Result: try our new coffee, bagels discontinued_items = menu.difference(new_menu)
print ('sorry, we no longer have', ', '.join(discontinued_items)) # Result: sorry, we no longer have panckes
old_items = new_menu.intersection(menu)
print ('Or get the same old', ', '.join(old_items)) # Result: Or ger the same old eggs, ham, bacon full_menu = new_menu.union(menu)
print ('At one time or another, we have served ', ','.join(full_menu))

3、namedtuple    生成可以使用名字来访问元素内容的tuple 子类,非常方便

import collectionshttp:
LightObject = collections.namedtuple('LightObject', ['shortname', 'otherprop'])
n = LightObject(shortname = 'something', otherprop = 'something else')
n.shortname # something

4、deque 双段队列,最大好处就是可以从头部添加和删除对象 popleft()、 appendleft()

import collections
d = collections.deque('')
print d.popleft() # '1'
d.appendleft('')
print d # deque(['7','2','3','4','5','6'])

5、Counter 同样是collections 中的,主要用来计数

import collections
c = collections.Counter('abcab')
print c #Couner({'a':2,'b':2,'c':1}

elements 方法返回一个迭代器,将生成Counter 知道的所有元素;most_common(n)生成一个序列,包含最常用的输入值及相应计数

Python Tips and Traps(一)的更多相关文章

  1. Python Tips and Traps(二)

    6.collections 模块还提供有OrderedDict,用于获取有序字典 import collections d = {'b':3, 'a':1,'x':4 ,'z':2} dd = col ...

  2. [转]Python tips: 什么是*args和**kwargs?

    Python tips: 什么是*args和**kwargs? 原文地址:http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html ...

  3. Python Tips阅读摘要

    发现了一本关于Python精通知识点的好书<Python Tips>,关于Python的进阶的技巧.摘录一些比较有价值的内容作为分享. *args and **kwargs 在函数定义的时 ...

  4. python tips(持续更新中)

    python tips 可变对象与不可变对象 在python中,可变对象有数值类型(int,float),字符串(str),元组(tuple),可变对象有列表(list),字典(dict),集合(se ...

  5. 【转载】Python tips: 什么是*args和**kwargs?

    转自Python tips: 什么是*args和**kwargs? 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwa ...

  6. python tips(持续更新)

    1. 引用上一层目录 import syssys.path.append('..')import xx 2. python json JSON是一种轻量级的数据交换格式.可以解决数据库中文存储问题,对 ...

  7. Python - Tips

    01 - input与raw_input的区别 input() #可以直接输入数字,但输入字符的要用引号''或者双引号"" raw_input() #将所有的输入都直接当作一串字符 ...

  8. Python tips: 什么是*args和**kwargs?

    推荐查看:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473844 ...

  9. Python tips(

    (此文是在实际工程中遇到的一些小问题,给予解决和整理.解决方法大多来自网上零散的文章.有一个系统化的Python问题解决方案,来自<Python 3 学习笔记>雨痕著,其中对Python的 ...

随机推荐

  1. G++ 教程(转)

    简介      gcc and g++分别是GNU的c & c++编译器 gcc/g++在执行编译工作的时候,总共需要4步  1.预处理,生成.i的文件[预处理器cpp]  2.将预处理后的文 ...

  2. Java的递归算法

    递归算法设计的基本思想是:对于一个复杂的问题,把原问题分解为若干个相对简单类同的子问题,继续下去直到子问题简单到可以直接求解,也就是说到了递推的出口,这样原问题就有递推得解. 关键要抓住的是: (1) ...

  3. HTML增加删除邮件(table)

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

  4. start mysqld on Mac server

    #!/bin/sh # Source the common setup functions for startup scripts test -r /etc/rc.common || exit 1 . ...

  5. Spring MVC中如何传递对象参数

    springController: @Controller @RequestMapping("/user") public UserController extends BaseC ...

  6. 大数据基础知识:分布式计算、服务器集群[zz]

    大数据中的数据量非常巨大,达到了PB级别.而且这庞大的数据之中,不仅仅包括结构化数据(如数字.符号等数据),还包括非结构化数据(如文本.图像.声音.视频等数据).这使得大数据的存储,管理和处理很难利用 ...

  7. C++学习指南

    转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...

  8. 基于ACE的定时器模板类

    ACETimerClockGenerator.h ClockGeneratorIF.h 在类中定义一个结构体,在结构体中定义一个函数. 在结构体中定义一个函数,这样做有什么好呢? TimerHandl ...

  9. 九度OJ 1113 二叉树

    题目地址:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...

  10. 虚拟机中如何Linux系统如何访问PC硬盘中的文件(如何将windows下的文件夹挂载到linux虚拟机下)

    这段时间决定学习嵌入式,变打算安装个Linux系统先熟悉一下Linux系统的使用,但自己电脑上安装的win7系统又不想装双系统,一是闲麻烦,二是由于对Linux系统不熟悉担心会因为自己的误操作而损坏系 ...