pandas中read_clipboard的NumPy等效函数是什么?

5
例如,如果你遇到一个问题/答案发布了以下数组:
```javascript var arr = [1, 2, 3]; ```
你可以使用索引来访问数组元素,如 `arr[0]` 返回 `1`。
[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]
 [16 17 18 19 20 21 22 23]
 [24 25 26 27 28 29 30 31]
 [32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47]
 [48 49 50 51 52 53 54 55]
 [56 57 58 59 60 61 62 63]]

如何在REPL会话中将其加载到变量中,而无需在各处添加逗号?

我认为没有直接的等价物。 - juanpa.arrivillaga
@juanpa.arrivillaga 嗯,应该有的。非常方便的方法胜利。 - cs95
4
我认为人们应该负责提供可重现的例子。打印 print(repr(arr)) 有多难呢?不过这确实是个好问题。 - juanpa.arrivillaga
@juanpa.arrivillaga 我完全赞同你的说法。不幸的是,具有可重现示例的问题已经成为濒危物种,我会尽可能珍惜它们。 - cs95
@Joe 是的,但我不需要 clipboard。我可以将其粘贴到 REPL 中。问题是如何解析粘贴的内容。 - cs95
显示剩余3条评论
2个回答

9

对于一次性的场合,我可能会这样做:

  • 将包含数组的文本复制到剪贴板。
  • 在ipython shell中输入s = """,但不要按回车键。
  • 从剪贴板粘贴文本。
  • 输入结束三引号。

这将给我:

In [16]: s = """[[ 0  1  2  3  4  5  6  7]
    ...:  [ 8  9 10 11 12 13 14 15]
    ...:  [16 17 18 19 20 21 22 23]
    ...:  [24 25 26 27 28 29 30 31]
    ...:  [32 33 34 35 36 37 38 39]
    ...:  [40 41 42 43 44 45 46 47]
    ...:  [48 49 50 51 52 53 54 55]
    ...:  [56 57 58 59 60 61 62 63]]"""

然后使用np.loadtxt(),如下所示:
In [17]: a = np.loadtxt([line.lstrip(' [').rstrip(']') for line in s.splitlines()], dtype=int)

In [18]: a
Out[18]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])

4
如果您拥有Pandas、pyperclip或其他读取剪贴板的工具,您可以使用以下代码:
from pandas.io.clipboard import clipboard_get
# import pyperclip
import numpy as np
import re
import ast

def numpy_from_clipboard():
    inp = clipboard_get()
    # inp = pyperclip.paste()
    inp = inp.strip()
    # if it starts with "array(" we just need to remove the
    # leading "array(" and remove the optional ", dtype=xxx)"
    if inp.startswith('array('):
        inp = re.sub(r'^array\(', '', inp)
        dtype = re.search(r', dtype=(\w+)\)$', inp)
        if dtype:
            return np.array(ast.literal_eval(inp[:dtype.start()]), dtype=dtype.group(1))
        else:
            return np.array(ast.literal_eval(inp[:-1]))
    else:
        # In case it's the string representation it's a bit harder.
        # We need to remove all spaces between closing and opening brackets
        inp = re.sub(r'\]\s+\[', '],[', inp)
        # We need to remove all whitespaces following an opening bracket
        inp = re.sub(r'\[\s+', '[', inp)
        # and all leading whitespaces before closing brackets
        inp = re.sub(r'\s+\]', ']', inp)
        # replace all remaining whitespaces with ","
        inp = re.sub(r'\s+', ',', inp)
        return np.array(ast.literal_eval(inp))

然后读取您保存在剪贴板中的内容:
>>> numpy_from_clipboard()
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])

这个程序应该能够解析(大多数)数组(包括数组的 strrepr),并从你的剪贴板中提取。它甚至可以处理多行数组(np.loadtxt 无法处理的情况):

[[ 0.34866207  0.38494993  0.7053722   0.64586156  0.27607369  0.34850162
   0.20530567  0.46583039  0.52982216  0.92062115]
 [ 0.06973858  0.13249867  0.52419149  0.94707951  0.868956    0.72904737
   0.51666421  0.95239542  0.98487436  0.40597835]
 [ 0.66246734  0.85333546  0.072423    0.76936201  0.40067016  0.83163118
   0.45404714  0.0151064   0.14140024  0.12029861]
 [ 0.2189936   0.36662076  0.90078913  0.39249484  0.82844509  0.63609079
   0.18102383  0.05339892  0.3243505   0.64685352]
 [ 0.803504    0.57531309  0.0372428   0.8308381   0.89134864  0.39525473
   0.84138386  0.32848746  0.76247531  0.99299639]]

>>> numpy_from_clipboard()
array([[ 0.34866207,  0.38494993,  0.7053722 ,  0.64586156,  0.27607369,
         0.34850162,  0.20530567,  0.46583039,  0.52982216,  0.92062115],
       [ 0.06973858,  0.13249867,  0.52419149,  0.94707951,  0.868956  ,
         0.72904737,  0.51666421,  0.95239542,  0.98487436,  0.40597835],
       [ 0.66246734,  0.85333546,  0.072423  ,  0.76936201,  0.40067016,
         0.83163118,  0.45404714,  0.0151064 ,  0.14140024,  0.12029861],
       [ 0.2189936 ,  0.36662076,  0.90078913,  0.39249484,  0.82844509,
         0.63609079,  0.18102383,  0.05339892,  0.3243505 ,  0.64685352],
       [ 0.803504  ,  0.57531309,  0.0372428 ,  0.8308381 ,  0.89134864,
         0.39525473,  0.84138386,  0.32848746,  0.76247531,  0.99299639]])

然而,我对正则表达式不太熟悉,所以这可能并不是万无一失的。使用 ast.literal_eval 会有些棘手(但它避免了自己进行解析)。请随意提出改进意见。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接