Replacing the values in a list with nested lists

Waffle05

How do I replace the values in a list with nested lists? For example:

array1 = [[1,2],[3,4],[5,6,7,8]]
array2 = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8]

With these arrays, I need to replace all the values of array1 with the values of array2. The final array will look like the following:

final_array = [[0.1,0.2],[0.3,0.4],[0.5,0.6,0.7,0.8]]
DevLounge

As you said you wanted to replace array1 sublists values by the values of array2, this will do what you want:

array1 = [[1, 2], [3, 4], [5, 6, 7, 8]]
array2 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]

it = iter(array2)
array1 = [[next(it) for _ in sublist] for sublist in array1]

This works because the length of array2 matches the sum of array1 sublists lengths and you kind of use use array1 as a template of list of sublists with various lenghts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Build a list of maximum values from a set of lists nested inside nested maps with lambda

Periodically replacing values in a list

Flatten nested lists in a list

Replacing null values in a list with values of another list

Replacing values in certain list in nested lists

Using a for loop to insert list values in empty lists within a nested dictionary

replacing string in list of lists python

How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)?

Replacing elements in arbitrary nested lists

Replacing the missing values in python with Lists in python dataframe

Replacing 'NA's in a nested list

Transform list of Pandas DataFrames to nested dictionary with values as lists

Creating list of lists from nested dictionary values (tree)

Subset (list of lists) nested Lists

Replacing values in column of list of lists in R

replacing an element in a list of lists in haskell

replacing an element in a list of lists -- haskell

replacing the values of a list in python

replacing class name in nested list

Python searching in nested lists to find values and insert values into a new list

list merging in nested lists

Replacing the last item inside of some nested lists with each consecutive item of another list

R replacing `for` by `sapply` in a list of lists

Replacing list values with for loop

Conditionally replacing specific values from a list of lists in R

Replacing values in a list of lists with dictionary values, pythonic or better way

Replacing nan values in a Pandas data frame with lists

Separate lists in nested list, and plotting first and second row values

Elegant way to split a list of strings into nested lists with values of type float