Numpy数组剪裁多行字符串

huangapple go评论72阅读模式
英文:

Numpy Array clips multiline string

问题

以下是您要翻译的内容:

我一直在尝试将多行字符串保存在一个类似这样的numpy数组中

但是当我稍后检查数组的内容时它是空的

我怀疑%可能会影响我但我不知道如何处理它

strings = np.empty(1, dtype=str) strings[0] = """         $_1 = 
    %_1.a =      25129 ± 92.3741
    $_2 = %_2.height =    11340.5 ± 1951.81
    $_3 = %_2.center =    63.4979 ± 0.275278
      $_4 = %_2.hwhm =     1.6318 ± 0.324661
    $_5 = %_3.height =    19482.3 ± 2420.92
    $_6 = %_3.center =    106.329 ± 0.12973
      $_7 = %_3.hwhm =    1.07347 ± 0.155327
    $_8 = %_4.height =    9985.67 ± 2382.35
    $_9 = %_4.center =    223.417 ± 0.257358
     $_10 = %_4.hwhm =   -1.11065 ± 0.30902
   $_11 = %_5.height =      61622 ± 2154.58
   $_12 = %_5.center =    443.983 ± 0.0458769
     $_13 = %_5.hwhm =      1.338 ± 0.0540433
   $_14 = %_6.height =    36949.9 ± 2230.42
   $_15 = %_6.center =    541.081 ± 0.0738621
     $_16 = %_6.hwhm =    1.24646 ± 0.086812
   $_17 = %_7.height =    28368.8 ± 2217.38
   $_18 = %_7.center =    693.312 ± 0.0968789
     $_19 = %_7.hwhm =    1.26497 ± 0.114331"""

print(strings[0])

这在我的情况下会得到一个空字符串而我希望它打印出字符串
英文:

I have been trying to save some multiline string in a numpy array like this:

however when i later check the content of the array it is empty

I suspect the % might be screwing me here but I dont know how to deal with it.

strings = np.empty(1, dtype=str) strings[0] = """         $_1 = 
%_1.a =      25129 ± 92.3741
$_2 = %_2.height =    11340.5 ± 1951.81
$_3 = %_2.center =    63.4979 ± 0.275278
$_4 = %_2.hwhm =     1.6318 ± 0.324661
$_5 = %_3.height =    19482.3 ± 2420.92
$_6 = %_3.center =    106.329 ± 0.12973
$_7 = %_3.hwhm =    1.07347 ± 0.155327
$_8 = %_4.height =    9985.67 ± 2382.35
$_9 = %_4.center =    223.417 ± 0.257358
$_10 = %_4.hwhm =   -1.11065 ± 0.30902
$_11 = %_5.height =      61622 ± 2154.58
$_12 = %_5.center =    443.983 ± 0.0458769
$_13 = %_5.hwhm =      1.338 ± 0.0540433
$_14 = %_6.height =    36949.9 ± 2230.42
$_15 = %_6.center =    541.081 ± 0.0738621
$_16 = %_6.hwhm =    1.24646 ± 0.086812
$_17 = %_7.height =    28368.8 ± 2217.38
$_18 = %_7.center =    693.312 ± 0.0968789
$_19 = %_7.hwhm =    1.26497 ± 0.114331"""
print(strings[0])

this gives an Empty string in my case while I would expect it to print the string.

答案1

得分: 0

In [1]: strings = np.empty(1, dtype=str)
Out[1]: strings = np.empty(1, dtype=str)

If I look at the result, I see that the dtype is 'U1'. That's one unicode character. print(strings) won't show this. It needs to be print(repr(strings)), which is what ipython shows:

如果我查看结果,我会发现dtype是'U1'。这只是一个Unicode字符。print(strings)不会显示这一点。需要使用print(repr(strings)),这是ipython所显示的:

In [2]: strings
Out[2]: array([''], dtype='<U1')

Any string assigned to this array will be clipped (some argue that it should at least give a warning):

将任何字符串分配给这个数组都将被截断(有人认为它至少应该发出警告):

In [3]: strings[0]='foobar'
In [4]: strings
Out[4]: array(['f'], dtype='<U1')

We can start out with a dtype that accepts longer strings:

我们可以从接受较长字符串的dtype开始:

In [5]: strings = np.empty(1, dtype='U100')
In [6]: strings
Out[6]: array([''], dtype='<U100')
In [7]: strings[0]='foobar'
In [8]: strings
Out[8]: array(['foobar'], dtype='<U100')

np.array(['foobar']) will select a dtype to fit the input, but then you can't replace that with longer strings.

np.array(['foobar'])将选择一个适合输入的dtype,但然后您无法将其替换为较长的字符串。

numpy isn't the best for strings. Python strings can be any length. Because of how it stores its data, numpy uses a fixed (or max) 'Un' dtype. Otherwise, it doesn't add much of its code for handling strings. So unless you really need the benefit of a multidimensional layout, you're better off just working with Python strings and lists.

numpy不是最适合字符串的工具。Python字符串可以是任何长度。由于它存储数据的方式,numpy使用固定(或最大)'Un' dtype。否则,它不会为处理字符串添加太多自己的代码。因此,除非您真的需要多维布局的好处,否则最好只使用Python字符串和列表。

In [9]: ['foobar']
Out[9]: ['foobar']

Your multiline string is just a string with '\n'.

您的多行字符串只是一个包含'\n'的字符串。

英文:
In [1]: strings = np.empty(1, dtype=str)

If I look at the result, I see that the dtype is 'U1'. That's one unicode character. print(strings) won't show this. It needs to be print(repr(strings)), which is what ipython shows:

In [2]: strings
Out[2]: array([&#39;&#39;], dtype=&#39;&lt;U1&#39;)

Any string assigned to this array will be clipped (some argue that it should atleast give a warning):

In [3]: strings[0]=&#39;foobar&#39;    
In [4]: strings
Out[4]: array([&#39;f&#39;], dtype=&#39;&lt;U1&#39;)

We can start out with an dtype that accepts longer strings:

In [5]: strings = np.empty(1, dtype=&#39;U100&#39;)    
In [6]: strings
Out[6]: array([&#39;&#39;], dtype=&#39;&lt;U100&#39;)    
In [7]: strings[0]=&#39;foobar&#39;    
In [8]: strings
Out[8]: array([&#39;foobar&#39;], dtype=&#39;&lt;U100&#39;)

np.array([&#39;foobar&#39;])` will select a dtype to fit the input, but then you can't replace that with longer strings.

numpy isn't the best for strings. Python strings can be any length. Because of how it stores its data, numpy uses a fixed (or max) 'Un' dtype. Otherwise it doesn't add much of its own code for handling strings. So unless you really need the benefit of a multidimensional layout, you're better off just working with python strings and lists.

In [9]: [&#39;foobar&#39;]
Out[9]: [&#39;foobar&#39;]

Your multiline string is just a string with '\n'.

huangapple
  • 本文由 发表于 2023年3月7日 22:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定