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. textarea文本域宽度和高度(width、height)自己主动适应变化处理

    文章来源:http://www.cnblogs.com/jice/archive/2011/08/07/2130069.html <HTML> <HEAD> <TITLE ...

  2. Top 10 Questions about Java Exceptions--reference

    reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This arti ...

  3. dom4j中对xml的查增

    package dom; import java.io.FileWriter;import java.util.Iterator; import org.dom4j.Document;import o ...

  4. Android(java)学习笔记159:Dalivk虚拟机的初始化过程

    1.初始化下面系统函数(调用dvmStartup函数初始化所有相关的函数) 开始学习虚拟机的初始化过程,先从dvmStartup函数开始,这个函数实现所有开始虚拟机的准备工作: dvmAllocTra ...

  5. jhipster

    Client side: yeoman,grunt,bower,angularjs Server side: maven,spring,spring mvc rest,spring data jpa

  6. 购物车非cookie版

    2015.11.26购物车,非cookie版 [点击来,你发现被骗了(笑哭,笑哭,笑哭,源代码的话,留下邮箱吧,是在不好找这一时半会儿的.)] Jsp通过反射机制获取bean中的标签,但其实,可以没有 ...

  7. Windows Thrift安装及HelloWorld

    Thrift是一个facebook开源的高效RPC框架,其主要特点是跨语言及二进制高效传输(除了二进制,也支持json等常用序列化机制),官网地址:http://thrift.apache.org 跨 ...

  8. linux-ssh远程后台执行脚本-放置后台执行问题(转)

    写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...

  9. jquery 可拖动进度条

    实现这个效果怎么弄呢? <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...

  10. 移动端和PC端通用的三级导航菜单栏

    免责声明: 本博客发布的所有信息资料都将尽可能注明出处.作者及日期,本人无意侵犯他人相关权益,如无意中侵犯了哪个媒体或个人的权益或知识产权,请留言或来信告之,本人将立即给予删除. Demo下载地址:h ...