#!/usr/bin/python
# -*- coding: UTF-8 -*-
#方法一
l = ['aadfds','dsa', 'dcver','weiry','11111']
l = [x for x in l if 'a' not in x] #列表解析
print l
#方法二
l = ['aadfds','dsa', 'dcver','weiry','11111']
l = filter(lambda x:'a' not in x, l) #filter
print l
......................
阅读全部 | 2011年10月28日 06:15
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def subString(s, length):
us = unicode(s, 'utf-8')
gs = us.encode('gb2312')
n = int(length)
t = gs[:n]
while True:
try:
unicode(t, 'gbk')
break
......................
阅读全部 | 2011年10月28日 06:14
# Author: Fred L. Drake, Jr.
# fdrake@acm.org
#
# This is a simple little module I wrote to make life easier. I didn't
# see anything quite like it in the library, though I may have overlooked
# something. I wrote this when I was trying to read some heavily nested
# tuples with fairly non-descriptive content. This is modeled very much
# after Lisp/Scheme - style pretty-printing of lists. If you find it
# useful, thank small children who sleep at night.
"""Support to pretty-print lists, tuples, & dictionaries recursively.
......................
阅读全部 | 2011年10月28日 06:13
#!/usr/bin/python
# -*- coding: UTF-8 -*-
l1 = [6,1,2,1,1,1,3,2,4,5,4,4,4,4,4,4,4,4,4]
print l1
l2 = list(set(l1))
print l2
l2.sort(key=l1.index)
print l2
阅读全部 | 2011年10月28日 06:12
#!/usr/bin/python
# -*- coding: UTF-8 -*-
l = [1,2,3,1,1,1,1,1,1,1,2,2,2,1,1,3]
d = {}
for i in l:
if not d.has_key(i):
d.setdefault(i, l.count(i))
print d
......................
阅读全部 | 2011年10月28日 06:11
#!/usr/bin/python
# -*- coding: UTF-8 -*-
d = {126:4, 110:3, 215:4, 106:4, 333:3, 98:3}
l = d.items()
print l
def mycmp(a, b):
if a[1] > b[1]:
return -1
elif a[1] < b[1]:
return 1
......................
阅读全部 | 2011年10月28日 06:10
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#常规方法,容易维护
for x in range(1,10):
for y in range(1,x+1):
print '%sx%s=%s'%(y,x,y*x),
print ''
#用列表解析一行搞定,不太易读
print '\n'.join([' '.join(['%sx%s=%s'%(y,x,y*x) for y in range(1,x+1)]) for x in range(1,10)])
阅读全部 | 2011年10月28日 05:45