简体   繁体   English

在 python 中键入许多变量

[英]Type casting many variables in python

Possibly wrong terrminologies here, please correct me.这里可能是错误的术语,请纠正我。

Problem: I have many objects, say lists of integers a=[1,3,7], b=[1,9], c... etc. I want to convert each to an np.array for instance.问题:我有很多对象,比如整数列表 a=[1,3,7]、b=[1,9]、c... 等等。例如,我想将每个对象转换为 np.array。

So far: What works: Explicitly convert individually: a=np.array(a) b=np.array(b)...到目前为止:有效的方法:单独显式转换:a=np.array(a) b=np.array(b)...

What did not work [np.array(x) for x in [a,b,c...]] which only gives a list of the arrays (ok, that was expected) but does not change the variable a,b... to be of type np.array.什么不起作用 [np.array(x) for x in [a,b,c...]] 只给出了 arrays 的列表(好的,这是预期的),但不会更改变量 a,b。 ..为 np.array 类型。

I am aware this unveals how little I know about python basics (what is pointer, what is reference etc...too long ago where i meartn about it for c++).我知道这表明我对 python 的基础知识知之甚少(什么是指针,什么是引用等……很久以前我就在 C++ 中了解它)。

I would be happy to clean up this question if anyone can give me some tips.如果有人可以给我一些提示,我很乐意清理这个问题。

cheers干杯

You have the right idea, but as you've seen, np.array(x) will create a new array based on x , not modify x itself.您有正确的想法,但正如您所见, np.array(x)将基于x创建一个新数组,而不是修改x本身。 Having said that, you can assign these values back to the original variables:话虽如此,您可以将这些值分配回原始变量:

a,b,c,etc=[np.array(x) for x in [a,b,c,etc]] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM