How to create a list of lists

My Python code generates a list every time it loops:

list = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)

But I want to save each of them - do I need a list of lists right?

So I tried:

list[i] = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)

But Python now tells me that the "list" is undefined. I am not sure how I define this. Also, is the list of lists the same as an array?

Thank!

+5
source share
4 answers

Use the append method, for example:

lst = []
line = np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1)
lst.append(line)
+4
source

You want to create an empty list, and then add the created list to it. This will give you a list of listings. Example:

>>> l = []
>>> l.append([1,2,3])
>>> l.append([4,5,6])
>>> l
[[1, 2, 3], [4, 5, 6]]
+16
source

, .

>>> list1 = []
>>> for i in range(10) :
...   list1.append( range(i,10) )
...
>>> list1
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9], [6, 7, 8, 9], [7, 8, 9], [8, 9], [9]]
+4

list - .

, ( ), , , -

my_list = []
my_list.append(np.genfromtxt('temp.txt', usecols=3, dtype=[('floatname','float')], skip_header=1))
my_list.append(np.genfromtxt('temp2.txt', usecols=3, dtype=[('floatname','float')], skip_header=1))

This will create a list (type of mutable array in python) called my_listwith the output of the method np.getfromtext()in the first two indexes.

The first may be a link with my_list[0], and the second withmy_list[1]

+2
source

All Articles