numpy 将字符串变量传递给函数np,数组

e0uiprwp  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(136)

有一个给定值my = "~obr0.isnull(), ~obr1.isnull(), ~obr2.isnull(), ~obr3.isnull(), ~obr4.isnull()"的字符串,这一行应该传递给itog = np.any(np.array([my]), axis=0),以便解释器像这样读取它itog = np.any(np.array([~obr0.isnull(), ~obr1.isnull(), ~obr2.isnull(), ~obr3.isnull(), ~obr4.isnull()]), axis=0)在代码执行期间,变量“my”将被更改,然后传递给np。数组
我尝试这样做itog = np.any(np.fromstring(my, dtype='uint8')),但它抛出一个错误“DeprecationWarning:不推荐使用fromstring的二进制模式,因为它在unicode输入上的行为令人惊讶。使用frombuffer代替”
尝试这样做itog = np.any(np.frombuffer(my, dtype='uint8')),但它抛出了一个错误“TypeError:需要类似字节的对象,而不是'str'“

bogh5gae

bogh5gae1#

np.any(np.array([eval(s) for s in my.split(',')]), axis=0)

会做你想做的事它适用于任何返回相同类型的逗号分隔语句字符串。

相关问题