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的 ...
随机推荐
- xml配置和基于java类的bean配置搭配使用
如果同时使用了xml配置,和java类的bean配置(当然估计项目中一般不会这样), 在初始化容器指定资源文件的时候可能会比较麻烦 此时我们可以把基于java类的bean配置整合到xml中,或xml的 ...
- 【VBA研究】变量定义的类型和实际赋值类型
作者:iamlaosong VBA中变量能够先定义后使用,也能够不定义直接使用.假设模块前面加了Option Explicit语句,则变量必须先定义后使用. 只是.实验发现.VBA对变量类型没有进行严 ...
- android datepicker源码
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- 常用linux命令和配置
find只查看文件和只查看目录 find -type f -name clexec find -type d -name clexec 解压rpm [root@sj_x861 2]# ls e ...
- Mac OS X 如何设置默认浏览器
有时候我们不希望在 Mac 中点击任何连接都打开的是 Safari,这需要修改默认浏览器设置,在 Mac OS X 中如何设置默认浏览器呢? 打开 Safari 的偏好设置,在「通用」选项卡中有「默认 ...
- 轮播图-JavaScript
轮播图一: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...
- 制作一个类似苹果VFL的格式化语言来描述UIStackView
在项目中总是希望页面上各处的文字,颜色,字体大小甚至各个视图控件布局都能够在发版之后能够修改以弥补一些前期考虑不周,或者根据统计数据能够随时进行调整,当然是各个版本都能够统一变化.看到这样的要求后,第 ...
- Eclipse SVN插件账号、密码修改
操作系统:win7 svn插件:Window -> Preferences -> Team -> SVN 修改方式: 1,删除C:\Users\用户名\AppData\Roaming ...
- Java8特性---关于Null
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5713941.html ...
- spring中@param和mybatis中@param使用区别
spring中@param /** * 查询指定用户和企业关联有没有配置角色 * @param businessId memberId * @return */ int selectRoleCount ...