首页    新闻    小组    威客    人才    下载    博客    代码贴    在线编程    论坛
代码贴随便看看Python
#  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     hide bbsi
#!/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     hide bbsi
#!/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     hide bbsi
#!/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     hide bbsi
#!/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     hide bbsi
上一页 17 18 19 20 21 22 23 24 25 26