Python: list of lists

前端 未结 7 1105
夕颜
夕颜 2020-11-27 04:38

Running the code

listoflists = []
list = []
for i in range(0,10):
    list.append(i)
    if len(list)>3:
        list.remove(list[0])
        listoflists.         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 05:19

    I came here because I'm new with python and lazy so I was searching an example to create a list of 2 lists, after a while a realized the topic here could be wrong... this is a code to create a list of lists:

    listoflists = []
    for i in range(0,2):
        sublist = []
        for j in range(0,10)
            sublist.append((i,j))
        listoflists.append(sublist)
    print listoflists
    

    this the output [ [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9)], [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)] ]

    The problem with your code seems to be you are creating a tuple with your list and you get the reference to the list instead of a copy. That I guess should fall under a tuple topic...

提交回复
热议问题