Python: list of lists

前端 未结 7 1149
夕颜
夕颜 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:05

    You're also not going to get the output you're hoping for as long as you append to listoflists only inside the if-clause.

    Try something like this instead:

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

提交回复
热议问题