Python Numpy:np.array 将字符串长度超过 30 的列表值转换为 ''
我有一个 pythonList 字符串:
pythonList= ['Pricing1', 'Expense_Inflation_Rate_Annual.fac', 'Vertical', 'MNT_INFL_PC']
当我尝试通过像 pythonList[1]
这样的索引访问所有字符串时,它们工作得很好。
我将数组转换为 numpyArray
,这样我就可以使用 np.where
函数,但现在当我尝试访问 numpyArray[1]< 等所有字符串时/code>
'Expense_Inflation_Rate_Annual.fac'
值现在保存 '
而其他 3 个则按预期工作。
我的转换代码很简单:
numpyArray = np.array(pythonList)
编辑:
为了了解更多上下文,我正在使用 Python 3.9.2,并通过测试发现 numpy 不喜欢长度超过 30 个字符的字符串,任何超过 30 个字符的字符串都会得到
这是我以前在 pythonList 中阅读的代码:
with open (r'c:\\temp\{}'.format(resultFileName)) as lp:
fileData = lp.readlines()
pythonList= []
for row in fileData:
pythonList.append(row.split(','))
numpyArray= np.array(pythonList)
如果有人可以提供更多详细信息,我们将不胜感激。
谢谢。
I have a pythonList of strings:
pythonList= ['Pricing1', 'Expense_Inflation_Rate_Annual.fac', 'Vertical', 'MNT_INFL_PC']
When I try to access all the strings by index like pythonList[1]
they work just fine.
I converted the array into a numpyArray
so I could use the np.where
function, but now when I try to access all the strings like numpyArray[1]
the 'Expense_Inflation_Rate_Annual.fac'
value now holds '<str_, len() = 33>'
instead while the other 3 work as intended.
My code for the conversion is simply:
numpyArray = np.array(pythonList)
Edit:
For more context I am using Python 3.9.2 and have discovered with testing that numpy does not like strings longer than 30 characters and anything more than 30 gets the <str_, len() = ##> input.
Here is the code I used to read in pythonList:
with open (r'c:\\temp\{}'.format(resultFileName)) as lp:
fileData = lp.readlines()
pythonList= []
for row in fileData:
pythonList.append(row.split(','))
numpyArray= np.array(pythonList)
If anyone could provide more detail on this, it would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚您从哪里获得
''
。如果我索引该字符串,结果是一个类型
str_` 且长度为 33 的对象。从
pythonList
我们得到类似的东西,只是类型略有不同:一个 Python 列表包含长度不同的
str
对象。当转换为 numpy 数组时,它们以Un
dtype 的字节形式存储。这里U33
是输入中最长字符串的长度。您提到使用
where
,但没有详细说明。numpy 字符串数组相对于字符串列表没有太多优势。
It's not clear where you get the
'<str_, len() = 33>'
. If I index that string, the result is an object oftype
str_` and length 33.From
pythonList
we get something similar, except the type is slightly different:A Python list contains
str
objects that vary in length. When converted to a numpy array, they are stored as bytes with aUn
dtype. HereU33
is the length of the longest string in the input.You mention using
where
, but don't elaborate.numpy
arrays of strings don't have a lot of advantages relative to lists of strings.一旦获得了
numpy.str_
元素的集合,您需要使用str()
来获取实际的字符串。例如:Once you have the collection of
numpy.str_
elements, you need to usestr()
to get the actual string. For example: