Python的原生doctest parser是用來對付那些討厭的再版提示。 :)
>>> 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
什麼都在那裏記事本+普照一樣最好的文本編輯器永遠。 – Shashank
@ShashankGupta:你能解釋一下嗎? – Casebash
Notepad ++可讓您從選定行數中刪除列。只需按住Alt鍵,突出顯示它並點擊刪除(幾乎是2秒鐘的操作),就可以從字面上取出所有這些點和空格。我確信有其他高級文本編輯器可以讓你做同樣的事情。 – Shashank