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

后端 未结 8 2026
鱼传尺愫
鱼传尺愫 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 20:49

    Here is an example I used recently, it was a real mind bender but hopefully it can help out some one!

    pivot_peaks is a list of pandas data frames some of them are duplicates.

    # Create a new list with only a single entry for each item
    new_list = []
    
    for obj_index, obj in enumerate(pivot_peaks):
        add_new_frame = False
    
        if len(new_list) == 0:
            new_list.append(obj)
        else:
            for item in new_list:
                if item.equals(obj):
                    add_new_frame = False
                    break
                else:
                    add_new_frame = True
    
            if add_new_frame == True:
                new_list.append(obj)
    
    0 讨论(0)
  • 2021-02-07 20:50

    You need to assign via indexes. Let's say you've got a list of lists, where the inner lists each have 5 items like you describe. If you want to iterate through them and change the value of the second item in each inner list, you could do something like:

    l = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]
    for i in l:
        i[1] = "spam"
    
    print l
    (output) [[0, "spam", 2, 3, 4], [5, "spam", 7, 8, 9], [10, "spam", 12, 13, 14]]
    
    0 讨论(0)
  • 2021-02-07 20:54

    You could use enumerate():

    for index, sublist in enumerate(execlist):
       if sublist[0] == mynumber:
           execlist[index][1] = myctype
           execlist[index][2] = myx
           execlist[index][3] = myy
           execlist[index][4] = mydelay
           # break
    

    You can remove the # if execlist only contains at most one sublist whose first item can equal mynumber; otherwise, you'll cycle uselessly through the entire rest of the list.

    And if the itemnumbers are in fact unique, you might be better off with a dictionary or at least an OrderedDict, depending on what else you intend to do with your data.

    0 讨论(0)
  • 2021-02-07 21:03

    Don't assign local variables in lists. In the loop

    for i in lis:
        i = 5
    

    Just sets the variable i to 5 and leaves the actual contents of the list unchanged. Instead, you have to assign it directly:

    for i in range(len(lis)):
         lis[i] = 5
    

    The same applied for lists of lists, although in this case the local variable doesn't have to be assigned so you can use the for...in construct.

    for i in listoflists:
         for i2 in range(len(i)):
              i[i2] = 5 #sets all items in all lists to 5
    
    0 讨论(0)
  • 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']] 
    
    0 讨论(0)
  • 2021-02-07 21:10

    The problem is that you are creating a copy of the list and then modifying the copy. What you want to do is modify the original list. Try this instead:

    for i in range(len(execlist)):
        if execlist[i][0] == mynumber:
             execlist[i][1] = myctype
             execlist[i][2] = myx
             execlist[i][3] = myy
             execlist[i][4] = mydelay
    
    0 讨论(0)
提交回复
热议问题