[PYTHON] Convert a multidimensional list (array) to one dimension

Create a new empty list (array) and copy the original elements one by one.

For python

test.py



# coding: utf-8

animal_list = ["Dog", ["Persia", "Munchkin", "Scottish fold"], "Human", ["hamster,Capybara"]];
new_list = []

for animal in animal_list:
    if type(animal) == list:
        new_list.extend(animal)
    else:
        new_list.append(animal)
        
print(new_list)

For js (gas)

test.gs


function make_array() {
 
  var array = ["Dog", ["Persia", "Munchkin", "Scottish fold"], "Human", ["hamster,Capybara"]];
  var new_array = [];
  
  for (var i = 0; i <= array.length - 1; i++) {
    if (typeof(array[i]) == "object") {
      array[i].map(function(text) { new_array.push(text) });
    } else {
      new_array.push(array[i]);
    }
  }
  
  Logger.log(new_array);

}

-The list is variable (there is no need to determine the size), but the array is invariant. (It is necessary to decide the size at the time of declaration) ・ Is it necessary to determine the size of the array in js?

Recommended Posts

Convert a multidimensional list (array) to one dimension
[Python] How to convert a 2D list to a 1D list
Convert dict to array
What do you like about how to convert an array (list) to a string?
Convert a slice object to a list of index numbers
[Python] Convert list to Pandas [Pandas]
Multidimensional array initialization of list
How to convert an array to a dictionary with Python [Application]
Convert A4 PDF to A3 every 2 pages
Convert list to DataFrame with python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
Convert a string to an image
A tool to convert Juniper config
numpy: I want to convert a single type ndarray to a structured array
When you want to sort a multidimensional list by multiple lines
[Django] Convert QuerySet to dictionary type list
python> Convert tuple to list> aList = list (pi_tuple)
Convert array (struct) to json with golang
[python] How to sort by the Nth Mth element of a multidimensional array
How to clear tuples in a list (Python)
Convert multiple jpg files to one PDF file
Write a script to convert a MySQL dump to TSV
A python amateur tries to summarize the list ②
[Python] Manipulating elements in a list (array) [Sort]
Convert strings to character-by-character list format with python
Convert to HSV
Python multidimensional array
How to check in Python if one of the elements of a list is in another list
How to convert / restore a string with [] in python
Various ways to extract columns in a NumPy array
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert a text file with hexadecimal values to a binary file
How to convert a class object to a dictionary with SQLAlchemy
I made a code to convert illustration2vec to keras model
Things to note when initializing a list in Python
Convert vector of integer values to one hot representation
How to convert a mel spectrogram back to a wav file
[Python] I want to make a nested list a tuple
[Python] Created a method to convert radix in 1 second
Convert elements of numpy array from float to int
Let Code Day54 Starting from Zero "1290. Convert Binary Number in a Linked List to Integer"