简体   繁体   中英

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.

So far: What works: Explicitly convert individually: 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.

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++).

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. 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]] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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