美文网首页
Python 编程

Python 编程

作者: 开罗酒吧 | 来源:发表于2017-05-19 00:32 被阅读0次
def f(x):
    def g(y):
        return y + x + 3 
    return g
nf1 = f(1)
nf2 = f(3)

print(nf1(1))
print(nf2(1))
The previous example returns the following output:
5
7

将列表中元素反转排序,比如下面这样

>>> x = [1,5,2,3,4]
>>> x.reverse()
>>> x
[4, 3, 2, 5, 1]

列传字符串:

>>> l2 = ['1','2','3','4','5']
>>> ''.join(l2)
'12345'

字符串反转:
方法一,使用[::-1]:
s = 'python'
print s[::-1]

方法二,使用reverse()方法:
l = list(s)
l.reverse()
print ''.join(l)
输出结果:
nohtyp
nohtyp

1。大数据量的list,要进行局部元素删除,尽量避免用del随机删除,非常影响性能,如果删除量很大,不如直接新建list,然后用下面的方法释放清空旧list。

2。对于一般性数据量超大的list,快速清空释放内存,可直接用 a = [] 来释放。其中a为list。

3。对于作为函数参数的list,用上面的方法是不行的,因为函数执行完后,list长度是不变的,但是可以这样在函数中释放一个参数list所占内存: del a[:],速度很快,也彻底:)

4.列出当前目录下的所有目录:

>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['.lein', '.local', '.m2', '.npm', '.ssh', '.Trash', '.vim', 'Applications', 'Desktop', ...]

5.列出所有的.py文件:

>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
['apis.py', 'config.py', 'models.py', 'pymonitor.py', 'test_db.py', 'urls.py', 'wsgiapp.py']

相关文章

网友评论

      本文标题:Python 编程

      本文链接:https://www.haomeiwen.com/subject/vovrxxtx.html