且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中运行类似于Matlab的一段代码

更新时间:2023-10-17 10:13:40

如果您真的在谈论 iPython

使用?入门

>>> ?
IPython -- An enhanced Interactive Python
=========================================

IPython offers a combination of convenient shell features, special commands
and a history mechanism for both input (command history) and output (results
caching, similar to Mathematica). It is intended to be a fully compatible
replacement for the standard Python interpreter, while offering vastly
improved functionality and flexibility.

At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.

MAIN FEATURES
-------------

* Access to the standard Python help. As of Python 2.1, a help system is
  available with access to object docstrings and the Python manuals. Simply
  type 'help' (no quotes) to access it.

* Magic commands: type %magic for information on the magic subsystem.

* System command aliases, via the %alias command or the configuration file(s).

* Dynamic object information:

  Typing ?word or word? prints detailed information about an object.  If
  certain strings in the object are too long (docstrings, code, etc.) they get
  snipped in the center for brevity.

  Typing ??word or word?? gives access to the full information without
  snipping long strings. Long strings are sent to the screen through the less
  pager if longer than the screen, printed otherwise.
  ....etc....

了解%magic

>>> %magic    
IPython's 'magic' functions
===========================

The magic function system provides a series of functions which allow you to
control the behavior of IPython itself, plus a lot of system-type
features. There are two kinds of magics, line-oriented and cell-oriented.

Line magics are prefixed with the % character and work much like OS
command-line calls: they get as an argument the rest of the line, where
arguments are passed without parentheses or quotes.  For example, this will
time the given statement::

        %timeit range(1000)

Cell magics are prefixed with a double %%, and they are functions that get as
an argument not only the rest of the line, but also the lines below it in a
separate argument.  These magics are called with two arguments: the rest of the
call line and the body of the cell, consisting of the lines below the first.
For example::

        %%timeit x = numpy.random.randn((100, 100))
        numpy.linalg.svd(x)

will time the execution of the numpy svd routine, running the assignment of x
as part of the setup phase, which is not timed.
...etc...

我的最爱

  • %edit-在外部编辑器中编辑某些内容(上一行,函数,我的文件)
  • %load-从文件加载数据,让您添加一些文本,然后运行它
  • %debug-调试崩溃的脚本
  • %history-显示您到目前为止所做的事情
  • %paste-从剪贴板粘贴并运行代码
  • %macro-这可能是您的问题的解决方案
  • %timeit-测量执行时间
  • %recall-也接近您的问题
  • %rerun-也可以尝试
  • 通常,如果我逐步了解iPython的帮助,那么我总是会在其中找到一些提高工作效率的方法.也尝试一下.

    In general, I am always finding some productivity booster in iPython if I walk through its help. Try it too.

    注意:我正在从控制台使用iPyhton,但其他UI版本应提供非常相似的功能,即使功能不完全相同.

    Note: I am using iPyhton from console, but other UI versions shall provide very similar, if not exactly the same functionality.