Python Tips and Traps(一)
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(一)的更多相关文章
- Python Tips and Traps(二)
6.collections 模块还提供有OrderedDict,用于获取有序字典 import collections d = {'b':3, 'a':1,'x':4 ,'z':2} dd = col ...
- [转]Python tips: 什么是*args和**kwargs?
Python tips: 什么是*args和**kwargs? 原文地址:http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html ...
- Python Tips阅读摘要
发现了一本关于Python精通知识点的好书<Python Tips>,关于Python的进阶的技巧.摘录一些比较有价值的内容作为分享. *args and **kwargs 在函数定义的时 ...
- python tips(持续更新中)
python tips 可变对象与不可变对象 在python中,可变对象有数值类型(int,float),字符串(str),元组(tuple),可变对象有列表(list),字典(dict),集合(se ...
- 【转载】Python tips: 什么是*args和**kwargs?
转自Python tips: 什么是*args和**kwargs? 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwa ...
- python tips(持续更新)
1. 引用上一层目录 import syssys.path.append('..')import xx 2. python json JSON是一种轻量级的数据交换格式.可以解决数据库中文存储问题,对 ...
- Python - Tips
01 - input与raw_input的区别 input() #可以直接输入数字,但输入字符的要用引号''或者双引号"" raw_input() #将所有的输入都直接当作一串字符 ...
- Python tips: 什么是*args和**kwargs?
推荐查看:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473844 ...
- Python tips(
(此文是在实际工程中遇到的一些小问题,给予解决和整理.解决方法大多来自网上零散的文章.有一个系统化的Python问题解决方案,来自<Python 3 学习笔记>雨痕著,其中对Python的 ...
随机推荐
- android FakeWindow的小应用大用途
android FakeWindow的小应用大用途 在windowmanager里面有一个FakeWindow,细致一看也就是一个透明的应用覆盖到屏幕的最前端,这样有什么优点呢?首先我们还是从应用的需 ...
- cocos2d-x jsb 防止触摸事件传递
在游戏中要实现消息弹窗,让用户点击确认,其他区域产生遮罩,阻挡下层的事件被点击到,这是个很常用的功能,在cocos2d-x中,可以通过为layer添加事件代理来实现: pDirector->ge ...
- autocommit=0
mysql; Query OK, rows affected (0.00 sec) mysql> create table test( a int); Query OK, rows affect ...
- dateTimePicker日期时间插件-----限定节假日调休的可选择性
需求:在项目中需要一款这样的日期插件,可以选择年月日,时分秒,对法定节假日不能选择,因法定节假日进行的调休可以选择: 现在使用的比较多的日期插件比如:Wdatepicker,jqueryUI的date ...
- MFC 设置窗口背景图片
//在Onpaint函数中加入如下代码 //----------给窗口设置背景图片---------------------------- CPaintDC dc(this); CRect myrec ...
- YII设置用户访问过滤
设置用户访问过滤 1.哪个控制器设置访问过滤,就在哪个控制器中添加如下代码 class XxxController extends Controller { //当前控制器是否使用过滤功能 publi ...
- HDU-1018(简单数学)
Big Number Problem Description In many applications very large integers numbers are required. Some o ...
- Repositories.EntityFramework 实现方式
今天记录一下自己的EntityFramework数据访问层.这里用通过泛型Repository的方式实现了数据的访问.先上一张结构图. Configuration文件夹里面的类是全部实体映射类.这些类 ...
- 首页的sitecontent地址
当无法出现工具栏时,利用url地址,可以方便直接进入sitecontent http://sp2013/sites/bentest/_layouts/15/viewlsts.aspx
- ACM——数的计数
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1050 数的计数 时间限制(普通/Jav ...