简体   繁体   English

嵌套循环中的嵌套列表

[英]Nested list in nested loop

like extremely new, so please bear with me.像极新的,所以请多多包涵。

Im trying to increment each element of a nested list by 1我试图将嵌套列表的每个元素增加 1

a straight forward list works fine:一个简单的列表工作正常:

a = [1,2,3,4,5,6,7,8]
for i in range(len(a)):
    a[i] += 1

but why doesn't it work with:但为什么它不适用于:

a = [[1, 2], [3, 4], [5, 6], [7, 8]]

what am i missing?我错过了什么?

Let's unroll the loop so we can inspect:让我们展开循环以便我们可以检查:

a = [1, 2, 3, 4, 5, 6, 7, 8]
i = 0
assert a[i] == 1  # the zeroeth element of a
a[i] += 1  # increment 1, making it 2
assert a[i] == 2
i = 1
# ... etc, repeat

contrast with对比

a = [[1, 2], [3, 4], [5, 6], [7, 8]]
i = 0
assert a[i] == [1, 2]  # the zeroeth element of a is now the list [1, 2]!
a[i] += 1  # TypeError! What's the logical equivalent of adding 1 to a list? There isn't one

It won't work as you have another list inside list a or nested list.它不起作用,因为您在列表 a 或嵌套列表中有另一个列表。 Therefore, you need nested loop: Following program would help:因此,您需要嵌套循环:以下程序将有所帮助:

a = [[1, 2], [3, 4], [5, 6], [7, 8]]
for i in range(len(a)):
    for j in range(len(a[i])):
        a[i][j] += 1

Hope it Helps!!!希望能帮助到你!!!

a = [[1, 2], [3, 4], [5, 6], [7, 8]]

Each item in the list is again a list.列表中的每个项目又是一个列表。 You need to traverse and incerement each of it individually.您需要单独遍历和递增每一个。 So, nested for loop can solve your problem.因此, nested for loop可以解决您的问题。

A more generic solution - Recursion更通用的解决方案 - 递归

### Using recursion - The level of nesting doesn't matter
def incrementor(arr):
    for i in range(len(arr)):
        if type(arr[i]) is list:
            incrementor(arr[i])
        else:
            arr[i] = arr[i] + 1



a = [[1, 2], [3, 4], [5, 6], [7, 8],9,10,11,12,[13,[14,15,16]],[[17,18],[19,[20,21]]]]

incrementor(a)        
print(a)

Output Output

[[2, 3], [4, 5], [6, 7], [8, 9], 10, 11, 12, 13, [14, [15, 16, 17]], [[18, 19], [20, [21, 22]]]] [[2, 3], [4, 5], [6, 7], [8, 9], 10, 11, 12, 13, [14, [15, 16, 17]], [[18, 19 ], [20, [21, 22]]]]

In the first iteration your a[i] += 1 would effectively be a[0] = [1, 2] + 1 .在第一次迭代中,您a[i] += 1实际上是a[0] = [1, 2] + 1 That doesn't exactly make sense.这完全没有意义。 You need to have a second, inner loop.你需要有第二个内循环。

Use nested for loops:使用嵌套的 for 循环:

for i in range(len(a)):
    for ii in range(len(a[i])):
        a[i][ii] += 1

Because you have nested list then you have to iterate that nested one again因为您有嵌套列表,所以您必须再次迭代该嵌套列表

Here a cool way to check if there list inside with recursion这是一个很酷的方法来检查里面是否有递归列表

a = [1,[2, 4],3,[4, 5],5,6,7,8]

def increment_a_list(some_list):
  for i in range(len(some_list)):
     if type(some_list[i]) is list: # if the element is a list then execute the function again with that element
        increment_a_list(some_list[i])
     else:
        some_list[i] += 1 # +1 to that element if it not a list
  return some_list

print(increment_a_list(a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM