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

后端 未结 8 2047
鱼传尺愫
鱼传尺愫 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: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.

提交回复
热议问题