ipython 常用命令

1.pylab模式启动ipython

ipython --pylab

2.tab自动补全

3.%run 执行py脚本

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

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

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

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

  • 计算一行代码到执行时间
In [1]: str = ['foo', 'foobar', 'baz', 'qux', 'python', 'test']*100000
In [2]: %time test1 = [x for x in str if x.startswith('foo')]
CPU times: user 279 ms, sys: 15.5 ms, total: 295 ms
Wall time: 287 ms
In [3]: %time test1 = [x for x in str if x[0:3] == 'foo']
CPU times: user 123 ms, sys: 4.71 ms, total: 128 ms
Wall 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 loop

In [11]: %timeit x[0:3] == "foo"
10000000 loops, best of 3: 182 ns per loop
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值