Convert sequence to array python

Here's an almost* vectorized boolean-indexing based approach that I have used in several other posts -

def boolean_indexing[v]:
    lens = np.array[[len[item] for item in v]]
    mask = lens[:,None] > np.arange[lens.max[]]
    out = np.zeros[mask.shape,dtype=int]
    out[mask] = np.concatenate[v]
    return out

Sample run

In [27]: v
Out[27]: [[1], [1, 2], [3, 6, 7, 8, 9], [4]]

In [28]: out
Out[28]: 
array[[[1, 0, 0, 0, 0],
       [1, 2, 0, 0, 0],
       [3, 6, 7, 8, 9],
       [4, 0, 0, 0, 0]]]

*Please note that this coined as almost vectorized because the only looping performed here is at the start, where we are getting the lengths of the list elements. But that part not being so computationally demanding should have minimal effect on the total runtime.

Runtime test

In this section I am timing DataFrame-based solution by @Alberto Garcia-Raboso, itertools-based solution by @ayhan as they seem to scale well and the boolean-indexing based one from this post for a relatively larger dataset with three levels of size variation across the list elements.

Case #1 : Larger size variation

In [44]: v = [[1], [1,2,4,8,4],[6,7,3,6,7,8,9,3,6,4,8,3,2,4,5,6,6,8,7,9,3,6,4]]

In [45]: v = v*1000

In [46]: %timeit pd.DataFrame[v].fillna[0].values.astype[np.int32]
100 loops, best of 3: 9.82 ms per loop

In [47]: %timeit np.array[list[itertools.izip_longest[*v, fillvalue=0]]].T
100 loops, best of 3: 5.11 ms per loop

In [48]: %timeit boolean_indexing[v]
100 loops, best of 3: 6.88 ms per loop

Case #2 : Lesser size variation

In [49]: v = [[1], [1,2,4,8,4],[6,7,3,6,7,8]]

In [50]: v = v*1000

In [51]: %timeit pd.DataFrame[v].fillna[0].values.astype[np.int32]
100 loops, best of 3: 3.12 ms per loop

In [52]: %timeit np.array[list[itertools.izip_longest[*v, fillvalue=0]]].T
1000 loops, best of 3: 1.55 ms per loop

In [53]: %timeit boolean_indexing[v]
100 loops, best of 3: 5 ms per loop

Case #3 : Larger number of elements [100 max] per list element

In [139]: # Setup inputs
     ...: N = 10000 # Number of elems in list
     ...: maxn = 100 # Max. size of a list element
     ...: lens = np.random.randint[0,maxn,[N]]
     ...: v = [list[np.random.randint[0,9,[L]]] for L in lens]
     ...: 

In [140]: %timeit pd.DataFrame[v].fillna[0].values.astype[np.int32]
1 loops, best of 3: 292 ms per loop

In [141]: %timeit np.array[list[itertools.izip_longest[*v, fillvalue=0]]].T
1 loops, best of 3: 264 ms per loop

In [142]: %timeit boolean_indexing[v]
10 loops, best of 3: 95.7 ms per loop

To me, it seems itertools.izip_longest is doing pretty well! there's no clear winner, but would have to be taken on a case-by-case basis!

Educative Answers Team

During programming, there will be instances when you will need to convert existing lists to arrays in order to perform certain operations on them [arrays enable mathematical operations to be performed on them in ways that lists do not].

Lists can be converted to arrays using the built-in functions in the Python numpy library.

numpy provides us with two functions to use when converting a list into an array:

  • numpy.array[]

  • numpy.asarray[]

1. Using numpy.array[]

This function of the numpy library takes a list as an argument and returns an array that contains all the elements of the list. See the example below:

import numpy as np
my_list = [2,4,6,8,10]
my_array = np.array[my_list]
# printing my_array
print my_array
# printing the type of my_array
print type[my_array]

2. Using numpy.asarray[]

This function calls the numpy.array[] function inside itself. See the definition below:

def asarray[a, dtype=None, order=None]:
    return array[a, dtype, copy=False, order=order]

The main difference between np.array[] and np.asarray[] is that the copy flag is false in the case of np.asarray[], and true [by default] in the case of np.array[].

This means that np.array[] will make a copy of the object [by default] and convert that to an array, while np.asarray[] will not.

The code below ​illustrates the usage of np.asarray[]:

import numpy as np
my_list = [2,4,6,8,10]
my_array = np.asarray[my_list]
# printing my_array
print my_array
# printing the type of my_array
print type[my_array]

Copyright ©2022 Educative, Inc. All rights reserved

How do you convert to array in Python?

import numpy as np..
my_list = [2,4,6,8,10].
my_array = np. array[my_list].
# printing my_array..
print my_array..
# printing the type of my_array..
print type[my_array].

How do I turn a list into a NumPy array?

To convert a Python list to a NumPy array, use either of the following two methods:.
The np. array[] function that takes an iterable and returns a NumPy array creating a new data structure in memory..
The np. asarray[] function that takes an iterable as argument and converts it to the array. The difference to np..

Can we convert list to NumPy array?

The similarity between an array and a list is that the elements of both array and a list can be identified by its index value. In Python lists can be converted to arrays by using two methods from the NumPy library: Using numpy. array[]

How do I turn a list into a matrix in Python?

Convert List to Matrix in Python.
Use a Loop and List Slicing to Convert a List to an Array or Matrix in Python..
Use the array[] Function From the Numpy Library to Convert a List to an Array or Matrix in Python..
Use the asarray[] Function From the Numpy Library to Convert a List to an Array or Matrix in Python..

Chủ Đề