【问题标题】:How to modify the content of a nested list?如何修改嵌套列表的内容?
【发布时间】:2019-10-18 12:23:35
【问题描述】:

我有一些嵌套列表,我希望能够使用修改发生位置的索引来修改它们。

此类列表的一个示例是:

l2 = ['Node_50',
      ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_20']],
      ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]
     ]

然后我有一个新的元素列表和一个包含索引的列表

lnew = ['Node_1', 'Node_40', 'Node_17']
indexes = [1, 3]

我想要一个函数来替换新值列表给出的索引处的列表元素。该函数必须这样做(对于此示例):

l2[1][3] = lnew

列表可以有任意数量的嵌套列表,因此索引的长度可能会改变。 该函数需要适用于任何嵌套列表和任意数量的索引。

【问题讨论】:

    标签: python list nested


    【解决方案1】:

    您不需要函数,只需将新列表分配到所需位置,它将替换以前的值。

    l2 = ['Node_50',
           ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_20']],
           ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]
          ]
    lnew = ['Node_1', 'Node_40', 'Node_17']
    

    之前

    l2[1][3]
    

    返回

    ['Node_20']
    

    然后替换它

    l2[1][3] = lnew
    

    之后

    l2[1][3]
    

    返回

    ['Node_1', 'Node_40', 'Node_17']
    

    这也可以通过函数

    来完成
    def myFUN(LIST, newLIST, indexes):
        i,j = indexes
        if i >= len(LIST):
            print("list index (" + str(i) + ") out of range")
            return
        elif j >= len(LIST[i]):
            print("list index (" + str(j) + ") out of range")
            return
        else:
            LIST[i][j] = newLIST
            return LIST
    

    现在

    myFUN(l2, lnew, indexes)
    

    返回

    ['Node_50', ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_1', 'Node_40', 'Node_17']], ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]]
    

    但是

    myFUN(l2, lnew, (4,1))
    

    返回

    list index (4) out of range
    

    myFUN(l2, lnew, (1,25))
    

    返回

    list index (25) out of range
    

    保持原列表不变

    对于python3

    def myFUN(LIST, newLIST, indexes):
        res = LIST.copy()
        i,j = indexes
        if i >= len(LIST):
            print("list index (" + str(i) + ") out of range")
            return
        elif j >= len(LIST[i]):
            print("list index (" + str(j) + ") out of range")
            return
        else:
            res[i][j] = newLIST
            return res
    

    在 python 2 中使用res = LIST[:]res=list(LIST)。现在

    myFUN(l2, lnew, indexes)
    

    返回

    ['Node_50', ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_1', 'Node_40', 'Node_17']], ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]]
    

    但 l2 保持不变

    l2
    

    返回

    ['Node_50', ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_20']], ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]]
    

    【讨论】:

    • 不是真的,让我澄清一下:我知道如何针对一个特定示例执行此操作,我想要的是一个可以执行此操作的函数,但可以使用任何原始列表、任何新列表和任何索引列表(它可以有很多值)
    • 你知道如何概括同样的事情吗?
    • @Daniel Casasampera,现在怎么样?
    • 无法想象一个不同的 list2 索引 = [1, 3, 5 ,4, 3, ...]
    • 所以你想通过索引 1,3,5,4,3 修改 l2?只需在函数内添加一个 for 循环来遍历所有索引,直到它到达最后一个。您应该考虑添加一个 mcve (stackoverflow.com/help/minimal-reproducible-example),以便我们完全解决您的问题
    【解决方案2】:

    更短的是:

    l2 = ['Node_50',
          ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_20']],
          ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]
         ]
    
    
    lnew = ['Node_1', 'Node_40', 'Node_17']
    indexes = [1, 3]
    
    
    def set_deep(root, indexes, value):
    
        for x in indexes[:-1]:
            root = root[x]
        root[indexes[-1]] = value
    
    
    set_deep(l2, indexes, lnew)
    
    
    print(l2)
    

    考虑一下,特性列表[iterable] 应该添加到 Python 中。 我认为实际上 numpy 支持 list[list] 表示法?

    【讨论】:

      【解决方案3】:

      我认为user8426627 已经给出了一个很好的答案,但如果你更喜欢函数式风格,你可以这样做:

      >>> from functools import reduce
      >>> from operator import getitem
      >>> reduce(getitem, indexes[:-1], l2)[indexes[-1]] = lnew
      >>> l2
      ['Node_50',
        ['Node_48', 'Node_23', ['Node_12', 'Node_3'], ['Node_1', 'Node_40', 'Node_17']],
        ['Node_22', ['Node_44'], ['Node_7', 'Node_40']]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        • 2021-02-21
        • 1970-01-01
        • 2011-01-25
        相关资源
        最近更新 更多