在列表的层次结构之后放置一个带有索引(改变长度)的列表以更改值
placing a list with indices (changing length) after a hierarchy of lists to change the values
我有以下列表层次结构:
boundaries = [[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
两个带索引的列表:
index1 = [1, 0, 2] # is the same as [1][0][2]
index2 = [1, 1, 0, 1] # is the same as [1][1][0][1]
但是,当我尝试进行下面的描述时,它不起作用。
boundaries[index1] = 45
boundaries[index2] = 45
这是不可能的,因为 index1
和 index2
是列表。
我该如何解决这个问题?
这可能是一个糟糕的设计......但是......你可以像这样迭代地下降到你的子列表中:
boundaries = [[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
index1 = [1, 0, 2] # is the same as [1][0][2]
index2 = [1, 1, 0, 1] # is the same as [1][1][0][1]
def iterateively_descent(idx, l, val):
# iteratively descent until the last sublist
for k in idx[:-1]:
l = l[k]
# set the value of the last sublist
l[idx[-1]] = val
print(boundaries)
iterateively_descent(index1,boundaries,45)
print(boundaries)
iterateively_descent(index2,boundaries,145)
print(boundaries)
输出:
[[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 145, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
我最初的想法与帕特里克建议的一样,这可以解决问题,但似乎有点奇怪。您是否考虑过改用 numpy 数组?如果这是一个选择,那么它可能会让你的生活更轻松一些。然后你可以这样做:
import numpy as np
boundaries = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
index = (0, 1, 1)
print("Before:")
print(boundaries)
boundaries[index] = 42
print("After: ")
print(boundaries)
输出将是:
Before:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
After:
[[[ 1 2 3]
[ 4 42 6]
[ 7 8 9]]]
这里的主要区别是您需要安装 numpy(不确定这是否是一个选项),并且您的索引必须是元组(我认为 numpy 不允许使用列表进行索引)。
我有以下列表层次结构:
boundaries = [[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
两个带索引的列表:
index1 = [1, 0, 2] # is the same as [1][0][2]
index2 = [1, 1, 0, 1] # is the same as [1][1][0][1]
但是,当我尝试进行下面的描述时,它不起作用。
boundaries[index1] = 45
boundaries[index2] = 45
这是不可能的,因为 index1
和 index2
是列表。
我该如何解决这个问题?
这可能是一个糟糕的设计......但是......你可以像这样迭代地下降到你的子列表中:
boundaries = [[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
index1 = [1, 0, 2] # is the same as [1][0][2]
index2 = [1, 1, 0, 1] # is the same as [1][1][0][1]
def iterateively_descent(idx, l, val):
# iteratively descent until the last sublist
for k in idx[:-1]:
l = l[k]
# set the value of the last sublist
l[idx[-1]] = val
print(boundaries)
iterateively_descent(index1,boundaries,45)
print(boundaries)
iterateively_descent(index2,boundaries,145)
print(boundaries)
输出:
[[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 145, 8, 7], [4, 3, 9, 8]]],
[[2, 1, 6, 5]], [[1, 0, 7, 6]]]
我最初的想法与帕特里克建议的一样,这可以解决问题,但似乎有点奇怪。您是否考虑过改用 numpy 数组?如果这是一个选择,那么它可能会让你的生活更轻松一些。然后你可以这样做:
import numpy as np
boundaries = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
index = (0, 1, 1)
print("Before:")
print(boundaries)
boundaries[index] = 42
print("After: ")
print(boundaries)
输出将是:
Before:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
After:
[[[ 1 2 3]
[ 4 42 6]
[ 7 8 9]]]
这里的主要区别是您需要安装 numpy(不确定这是否是一个选项),并且您的索引必须是元组(我认为 numpy 不允许使用列表进行索引)。