ipython 常用命令

来源:互联网 发布:神舟软件科技有限公司 编辑:程序博客网 时间:2024/06/07 11:33

1.pylab模式启动ipython

ipython --pylab

2.tab自动补全

3.%run 执行py脚本

test.py
def test(x):    return x + 1tmp = test(5)
In [1] : %run test.pyIn [2] : tmpOut[2] : 6

4.“!”使用系统shell执行命令

  • 使用系统shell执行命令
In [3] : !pwd/User/***/codespace
  • 将系统shell执行命令后到输出值赋给变量
In [5] : a = !pwdIn [6] : aOut[6] : ['/User/***/codespace']

5.“%time”计算代码执行时间

  • 计算一行代码到执行时间
In [1]: str = ['foo', 'foobar', 'baz', 'qux', 'python', 'test']*100000In [2]: %time test1 = [x for x in str if x.startswith('foo')]CPU times: user 279 ms, sys: 15.5 ms, total: 295 msWall time: 287 msIn [3]: %time test1 = [x for x in str if x[0:3] == 'foo']CPU times: user 123 ms, sys: 4.71 ms, total: 128 msWall time: 127 ms
  • 自动多次执行并计算执行平均时间
In [9]: x = "foobar"In [10]: %timeit x.startswith("foo")The slowest run took 4.49 times longer than the fastest. This could mean that an intermediate result is being cached.1000000 loops, best of 3: 425 ns per loopIn [11]: %timeit x[0:3] == "foo"10000000 loops, best of 3: 182 ns per loop
原创粉丝点击