`
phinecos
  • 浏览: 343528 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

每日学习笔记(4)

阅读更多

1,字典的输出

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->person={'name':'james','age':22}
print("%(name)s,%(age)d"%person)

importstring
person
={'name':'james','age':22}
t
=string.Template("$nameis$age")
print(t.substitute(person))

2python模块的安装

假设有下述MyClass模块,

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->classMyClass:
def__init__(self,name,age):
self.name
=name
self.age
=age
defprintInfo(self):
print"%s,%d"%(self.name,self.age)

if__name__=='__main__':
c
=MyClass()
c.printInfo()

我们可以使用distutils来为其制作安装包,首先需要创建一个名为setup.py的安装脚本,脚本内容如下:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->fromdistutils.coreimportsetup

setup(name
='MyClass',
version
='1.0',
py_modules
=['MyClass'],
)

然后执行下述命令:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->pythonsetup.pysdist

这样就产生了MANIFESTdist目录,后者包含了MyClass-1.0.tar.gz。我们可以将MyClass-1.0.tar.gz置于了另一个linux系统来安装此模块,

首先解压缩:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->gunzipMyClass-1.0.tar.gz
tarxvfMyClass-
1.0.tar

解压完成后,可以看到此压缩包中包含了MyClass.py,setup.py,PKG-INFO三个文件

然后用下述命令来完成安装:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->pythonsetup.pyinstall

3,listremove方法只是删除找到的第一个元素,并不是删除全部

4,若一个truple只包含一个元素,则应当在此元素后有一个逗号,例如 x = (True,)

5,print函数可以用来进行io重定向,

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->f=open('d:/1.txt','w')
print('hello','world',sep=',',file=f)
f.close()

6,python中的True,False很有趣,比如 0 in[True,False] 会返回什么值呢?这篇文章python源码的角度对此进行了分析,非常深刻

7,List Comprehension

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->a=[1,2,3]
[num
*2fornumina]

我们从右往左看,首先对a进行遍历,每次将一个元素赋给变量num,然后将num*2的值添加到返回列表中,甚至还可以这样:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->importos,glob
[file
forfileinglob.glob(‘*.py’)ifos.stat(file).st_size>6000]

这就只返回搜索到的py文件中大小大于6000的文件

8,lambda函数:

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->importos

defsplit_fully(path):
parent_path,name
=os.path.split(path)
ifname=='':
return(parent_path,)
else:
returnsplit_fully(parent_path)+(name,)
deffilterRule(num):
return(num%2==0);

defNormalFilter():
arr
=[1,2,3,4,6,7,8]
result
=filter(filterRule,arr)
printresult
defFilterByLambda():
arr
=[1,2,3,4,6,7,8]
result
=filter(lambdax:x%2==0,arr)
printresult
defFilterByLambda_2():
arr
=[1,2,3,4,6,7,8]
ruleFunc
=lambdax:x%2==0
result
=filter(ruleFunc,arr)
printresult
deftestMap():
arr
=[1,2,3,4,5,6]
result
=map(lambdax:"thenumis%d"%x,arr)
printresult
deftestMapWithList():
arr
=[[1,2,3],[4,5,6],[7,8,9]]
result
=map(lambdalist:[list[1],list[0],list[2]],arr)
printresult

if__name__=="__main__":
path
=split_fully("/home/phinecos")
printpath
NormalFilter()
FilterByLambda()
FilterByLambda_2()
testMap()
testMapWithList()

有一点值得注意,在python3.1中map和filter函数返回的是一个iterator,但以前的版本返回的是一个list,因此,若使用python3.1,则输出的语句应该改成:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->print(*result)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics