2

我想从Anaconda shell中的示例运行 Python 代码。不幸的是,我要粘贴的语句以. 有没有一种简单的方法来运行这样的语句而无需手动删除?我知道存在其他 shell,但我不想让它们与 Anaconda 一起使用......

>>> features  = array([[ 1.9,2.3],
...                    [ 1.5,2.5],
...                    [ 0.8,0.6],
...                    [ 0.4,1.8],
...                    [ 0.1,0.1],
...                    [ 0.2,1.8],
...                    [ 2.0,0.5],
...                    [ 0.3,1.5],
...                    [ 1.0,1.0]])
4

2 回答 2

5

Python 的原生doctest 解析器用于处理那些讨厌的 repr 提示。:)

>>> from doctest import DocTestParser
>>> repr_code = '''
... >>> features  = array([[ 1.9,2.3],
... ...                    [ 1.5,2.5],
... ...                    [ 0.8,0.6],
... ...                    [ 0.4,1.8],
... ...                    [ 0.1,0.1],
... ...                    [ 0.2,1.8],
... ...                    [ 2.0,0.5],
... ...                    [ 0.3,1.5],
... ...                    [ 1.0,1.0]])
... '''
>>> p = DocTestParser()
>>> code = next(filter(None, p.parse(repr_code.strip()))) # Filter out the useless parts
>>> print(code.source)
features  = array([[ 1.9,2.3],
               [ 1.5,2.5],
               [ 0.8,0.6],
               [ 0.4,1.8],
               [ 0.1,0.1],
               [ 0.2,1.8],
               [ 2.0,0.5],
               [ 0.3,1.5],
               [ 1.0,1.0]])
>>> array = list # Because it's cheaper than numpy
>>> exec(code.source) # If you're feeling very lucky...
>>> len(features)
9    
于 2013-09-23T01:26:58.023 回答
2

据我正确理解你:你想删除点。我会这样做:

  1. 打开vim
  2. i
  3. 粘贴文本 ( ctrl+v)
  4. ESC退出Insert mode
  5. ctrl+v进入Visual Block
  6. 向上移动光标以选择所有点
  7. x删除它们。

比用这个格式化的文本做任何你想做的事情(可以复制和粘贴)

于 2013-09-23T01:27:58.090 回答