首页 > 代码库 > Python11-2
Python11-2
列表按列排序(list sort)
如果列表的每个元素都是一个元组(tuple),我们要根据元组的某列来排序的化,可参考如下方法
下面例子我们是根据元组的第2列和第3列数据来排序的,而且是倒序(reverse=True)
>>> a = [(‘2011-03-17‘, ‘2.26‘, 6429600, ‘0.0‘), (‘2011-03-16‘, ‘2.26‘, 12036900, ‘-3.0‘), (‘2011-03-15‘, ‘2.33‘, 15615500,‘-19.1‘)]>>> print a[0][0]2011-03-17>>> b = sorted(a, key=lambda result: result[1],reverse=True)>>> print b[(‘2011-03-15‘, ‘2.33‘, 15615500, ‘-19.1‘), (‘2011-03-17‘, ‘2.26‘, 6429600, ‘0.0‘),(‘2011-03-16‘, ‘2.26‘, 12036900, ‘-3.0‘)]>>> c = sorted(a, key=lambda result: result[2],reverse=True)>>> print c[(‘2011-03-15‘, ‘2.33‘, 15615500, ‘-19.1‘), (‘2011-03-16‘, ‘2.26‘, 12036900, ‘-3.0‘),(‘2011-03-17‘, ‘2.26‘, 6429600, ‘0.0‘)]
sorted:http://www.cnblogs.com/65702708/archive/2010/09/14/1826362.html
lambda: http://www.cnblogs.com/coderzh/archive/2010/04/30/python-cookbook-lambda.html
命令行参数解析(getopt)
通常在编写一些日运维脚本时,需要根据不同的条件,输入不同的命令行选项来实现不同的功能 在Python中提供了getopt模块很好的实现了命令行参数的解析,下面距离说明。请看如下程序:
#!/usr/bin/env python# -*- coding: utf-8 -*-import sys,os,getoptdef usage():print ‘‘‘‘‘Usage: analyse_stock.py [options...]Options:-e : Exchange Name-c : User-Defined Category Name-f : Read stock info from file and save to db-d : delete from db by stock code-n : stock name-s : stock code-h : this help infotest.py -s haha -n "HA Ha"‘‘‘ try:opts, args = getopt.getopt(sys.argv[1:],‘he:c:f:d:n:s:‘)except getopt.GetoptError:usage()sys.exit()if len(opts) == 0:usage()sys.exit() for opt, arg in opts:if opt in (‘-h‘, ‘--help‘): usage() sys.exit()elif opt == ‘-d‘: print "del stock %s" % argelif opt == ‘-f‘: print "read file %s" % argelif opt == ‘-c‘: print "user-defined %s " % argelif opt == ‘-e‘: print "Exchange Name %s" % argelif opt == ‘-s‘: print "Stock code %s" % argelif opt == ‘-n‘: print "Stock name %s" % arg sys.exit()
Python 捕获用户 Ctrl+C ,Ctrl+D 事件
有些时候,需要在程序中捕获用户键盘事件,比如ctrl+c退出,这样可以更好的安全退出程序
try: do_some_func()except KeyboardInterrupt: print "User Press Ctrl+C,Exit"except EOFError: print "User Press Ctrl+D,Exit"
列表切割赋值(删除我知道,没想到插入也行)
>>> a = [1, 2, 3, 4, 5]>>> a[2:3] = [0, 0]>>> a[1, 2, 0, 0, 4, 5]>>> a[1:1] = [8, 9]>>> a[1, 8, 9, 2, 0, 0, 4, 5]>>> a[1:-1] = []>>> a[1, 5]
用压缩器反转字典
>>> m = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4}>>> m.items()[(‘a‘, 1), (‘c‘, 3), (‘b‘, 2), (‘d‘, 4)]>>> zip(m.values(), m.keys())[(1, ‘a‘), (3, ‘c‘), (2, ‘b‘), (4, ‘d‘)]>>> mi = dict(zip(m.values(), m.keys()))>>> mi{1: ‘a‘, 2: ‘b‘, 3: ‘c‘, 4: ‘d‘}
字典推导- -
>>> m = {x: x ** 2 for x in range(5)}>>> m{0: 0, 1: 1, 2: 4, 3: 9, 4: 16} >>> m = {x: ‘A‘ + str(x) for x in range(10)}>>> m{0: ‘A0‘, 1: ‘A1‘, 2: ‘A2‘, 3: ‘A3‘, 4: ‘A4‘, 5: ‘A5‘, 6: ‘A6‘, 7: ‘A7‘, 8: ‘A8‘, 9: ‘A9‘}
操作集合
>>> A = {1, 2, 3, 3}>>> Aset([1, 2, 3])>>> B = {3, 4, 5, 6, 7}>>> Bset([3, 4, 5, 6, 7])>>> A | Bset([1, 2, 3, 4, 5, 6, 7])>>> A & Bset([3])>>> A - Bset([1, 2])>>> B - Aset([4, 5, 6, 7])>>> A ^ Bset([1, 2, 4, 5, 6, 7])>>> (A ^ B) == ((A - B) | (B - A))
python高级数据结构(熟练使用,生活更轻松):http://blog.jobbole.com/65218/
Python11-2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。