Converting list of list of Int32 values to list of list of int values

1.9k Views Asked by At

I have a list of lists which contain numpy int32 values. I would like to convert all of these int32 values to regular int. The reason is because as a part of my process these values are fed into a non max suppression function later on, which won't accept int32.

Below is what the structure of what my data looks like (in regular int form). I don't know how to make a test set for int32 values... Or else I would probably have this problem figured out.

List of list of ints

test_list = [[1,2,3,4],[5,6,7,8]]

Edit: Some screenshots to help understand what my data looks like.

enter image description here enter image description here

2

There are 2 best solutions below

2
On BEST ANSWER

This should work:

In [167]: [[int(x) for x in sublist] for sublist in test_list]
Out[167]: [[1, 2, 3, 4], [5, 6, 7, 8]]
8
On

Examples of a non-iterative conversion.

Use the following to keep the structure as an ndarray:

a.astype(int)

>>> array([[1, 2, 3, 4],
           [5, 6, 7, 8]])

Or even more simply, use this to convert the ndarray to a list:

a.tolist()

>>> [[1, 2, 3, 4], [5, 6, 7, 8]]

Background:

You mention that your list is of int32 values. As such, it's highly likely that the 'list' is actually a numpy.ndarray. Here is an example:

a = np.array([[1,2,3,4],[5,6,7,8]], dtype=np.int32)

>>> array([[1, 2, 3, 4],
           [5, 6, 7, 8]], dtype=int32)

If this is the case, then converting the ndarray to a 'normal' list of int values is as simple as the two examples shown above.