Python - How to change values in a list of lists?

后端 未结 8 2015
鱼传尺愫
鱼传尺愫 2021-02-07 20:14

I have a list of lists, each list within the list contains 5 items, how do I change the values of the items in the list? I have tried the following:

    for [ite         


        
8条回答
  •  后悔当初
    2021-02-07 21:08

    Variable unpacking does not seem to pass the reference but copies the values. An solution would be to do it like this:

    foo = [[1, "gggg"], [3, "zzzz"]]
    
    for item in foo:
        item[0] = 2
        item[1] = "ffff"
    
    print(foo)
    
    >>>> [[2, 'ffff'], [2, 'ffff']] 
    

提交回复
热议问题