This is one of my favorite Python things to do, and it is so simple and easy to do that it is hard not to do it once in a while.
That’s because, as a matter of fact, it is so easy. The reason you need to do it is because in Python arrays are stored as integers. Now, this is generally not a big deal, but in this case, the reason it matters is that arrays allow you to store arbitrary data in memory. For example, you can store your favorite color as a string and have it work like a color.
The process of converting a string is so simple, but there are many more things that you should know. For example, if you’re trying to convert a string to a number, you would first convert it to float, then float-to-number, and then convert that string back to the original string.
Numpy is a Python library for manipulating and extracting data from arrays. So, for example, if you have a number, you can get it back into its original string again by just changing the first character to a 0. If the first character is a 0, then you have a number, so you just change the first character to a 1, and then you have a string again.
The last part is the most confusing part. To convert an array to an integer, you use a “composite” function to convert one array into another. In this case, we’re converting an numpy array of lists to an int array of lists.
What if you want to convert a numpy array of lists to an int array of lists? Well, you can’t do that because NumPy does not support arrays of integer. You’ll need to convert it to int first and then then array. So, you’d do numpy.array(list1) to get a list of integers, then convert it to a np.intarray with np.array.from_numpy(list1) and then use it as a np.
The numpy documentation has a pretty detailed list of all the functions you can use. I won’t repeat them here. Instead, I’ll cover a couple examples I use in a few projects.
First, you can pass in a list of integers and get back a list of lists of integers.