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,getopt
def 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 info
test.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" % arg
elif opt == ‘-f‘:
  print "read file %s" % arg
elif opt == ‘-c‘:
  print "user-defined %s " % arg
elif opt == ‘-e‘:
  print "Exchange Name %s" % arg
elif opt == ‘-s‘:
  print "Stock code %s" % arg
elif 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}
>>> A
set([1, 2, 3])
>>> B = {3, 4, 5, 6, 7}
>>> B
set([3, 4, 5, 6, 7])
>>> A | B
set([1, 2, 3, 4, 5, 6, 7])
>>> A & B
set([3])
>>> A - B
set([1, 2])
>>> B - A
set([4, 5, 6, 7])
>>> A ^ B
set([1, 2, 4, 5, 6, 7])
>>> (A ^ B) == ((A - B) | (B - A))

 python高级数据结构(熟练使用,生活更轻松):http://blog.jobbole.com/65218/

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。