how to create a list of lists

后端 未结 5 2081
孤独总比滥情好
孤独总比滥情好 2020-12-16 20:30

My Python code generates a list everytime it loops:

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


        
5条回答
  •  失恋的感觉
    2020-12-16 20:54

    Create your list before your loop, else it will be created at each loop.

    >>> 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]]
    

提交回复
热议问题