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. linux下查看本机socket端口详细信息

    netstat -paut [root@OA-JRY-SY-FDEP1 nginx-]# netstat -paut Active Internet connections (servers and ...

  2. [Javascript] Adding Shapes to Maps with Leaflet and GeoJSON

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 可拖拽重排的CollectionView

    来源:wazrx 链接:http://www.jianshu.com/p/8f0153ce17f9 写在前面 这段时间都在忙新项目的事儿,没有时间倒腾,这两天闲下来,想着一直没有细细的研究Collec ...

  4. 基于anyrtc的sdk实现直播连麦互动

    基于anyrtc的sdk实现直播连麦互动 前言 1.由于粘贴了较大的代码,造成内容比较长,可能会花费您较长的时间. 2.项目里面没有做权限判断,所以如果发现有页面发生崩溃可能是权限没有打开,请打开权限 ...

  5. Android源码解析--Quick Search in Settings

    欢迎大家加入群里交流:429664282 基于:android-6.0.1_r17 f4b8ad6 Android Settings中存在一个SearchIndexablesProvider,它提供了 ...

  6. Hibernate的fetch (转)

    fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例: fetch = "select" ...

  7. 第十篇:web之前端之django一些feature

    前端之django一些feature   前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. ...

  8. 11.14 noip模拟试题

      题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer ...

  9. flash&nbsp;wmode=&quot;window&amp;qu…

    引用一段话:opaque和tranparent由于都是无窗口内渲染模式,能很好的实现各层的遮挡,和一般div元素没有太大区别,而window.direct模式在现在看来,是没有任何办法实现被DHTML ...

  10. PHP Fatal error问题处理

    今天一个朋友公司网站遇到一个关于PHP的问题: PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried to ...