简体   繁体   English

替换列表列表中的每个第n个元素

[英]Replacing every nth element in list of lists

I have a numerical list of list 我有一个列表的数字列表

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

and I want to replace every nth element with 0 to get something like this (n=3 in this case) 我想用0替换每个第n个元素得到这样的东西(在这种情况下n = 3)

a = [[0,2,3,0,5,6],
     [0,8,9,0,11,12],
     [0,14,15,0,17,18]]

I am hoping to to it something like this 我希望能有这样的事情

a = [[i, j if j %n == 0 else i] for i, j in a]

but I can't figure the arguments it needs. 但我无法想象它需要的论据。

You can use a ternary if - else inside the inner list comprehension and enumerate to access the index with each element as tuples: 您可以在内部列表理解中使用三元 if - elseenumerate以使用每个元素作为元组来访问索引:

n = 3
result = [[x if i % n else 0 for i, x in enumerate(y)] for y in a]

Applied to your example list, 应用于您的示例列表,

>>> a = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
>>> n = 3
>>> [[x if i % n else 0 for i, x in enumerate(y)] for y in a]
[[0, 2, 3, 0, 5, 6], [0, 8, 9, 0, 11, 12], [0, 14, 15, 0, 17, 18]]

Try this code. 试试这个代码。

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

n=3
for out in range(0,len(a)):
    for i in range(0,len(a[out])):
        if i == n:
            a[out][i] = 0

print(a)

What about slicing the list and creating a new one with 0? 切片列表并用0创建一个新列表怎么样?

n=3; a = [l[:n] + [0] + l[n+1:] for l in a]

ggorlen has the right approach, but just to elaborate for your understanding and others who wanna learn list comprehensions.. ggorlen有正确的方法,但只是为了你的理解和其他想要学习列表理解的人。

First you need to iterate over each of the items in the list - 首先,您需要遍历列表中的每个项目 -

[item for item in l]

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

Next, you have a condition that is applied on the index (position must be 3), but you need to modify the element itself as well. 接下来,您有一个应用于索引的条件(位置必须为3),但您也需要修改元素本身。 So you need to iterate over both the index and the element together. 因此,您需要一起迭代索引和元素。

[[(i,n) for i,n in enumerate(item)] for item in l]

[[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)],
 [(0, 7), (1, 8), (2, 9), (3, 10), (4, 11), (5, 12)],
 [(0, 13), (1, 14), (2, 15), (3, 16), (4, 17), (5, 18)]]

Each tuple is (index, element). 每个元组都是(索引,元素)。 Lastly you need your condition. 最后你需要你的条件。 There are multiple ways for using if conditions in a list comprehension, but for your case you need to check the index in the (index, element) tuple and see if it returns a remainder when divided by 3. If yes, then return the element else return 0. 在列表推导中使用if条件有多种方法,但对于你的情况,你需要检查(index,element)元组中的索引,看看它是否在除以3时返回余数。如果是,则返回元素否则返回0。

[[n if i%3 else 0 for i,n in enumerate(item)] for item in l]

[[0, 2, 3, 0, 5, 6], [0, 8, 9, 0, 11, 12], [0, 14, 15, 0, 17, 18]]

Hope this explanation helps you with future problems like this. 希望这个解释可以帮助您解决此类未来的问题。

Cheers. 干杯。

Solution

a = [[0 if index==0 else j for index, j in enumerate(i)] for i in a]

>>> a = [[1,2,3,4,5,6], [7,8,9,10,11,12], [13,14,15,16,17,18]]
>>> a = [[0 if index==0 else j for index, j in enumerate(i)] for i in a]
>>> a
[[0, 2, 3, 4, 5, 6], [0, 8, 9, 10, 11, 12], [0, 14, 15, 16, 17, 18]]

In Python 3.8 you will introduce Assignment Expressions. 在Python 3.8中,您将介绍Assignment Expressions。

It is a new symbol := that allows assignment in (among other things) comprehensions. 这是一个新的符号:=允许在(除其他事项)理解中进行分配。

Although it would take more than one line of code, you could create a generalized solution by doing it like shown below, which would modify the list-of-lists inplace and therefore be more efficient than recreating the whole thing just to change a few values. 虽然要的码多行,你可以通过做如下所示创建一个通用的解决方案,这将就地修改列表的-名单,因此比重建整个事情更高效的只是更改了几个值。

def replace_nth(list_of_lists, n, value):
    for sublist in list_of_lists:
        for i, v in enumerate(sublist):
            if not i % n:
                sublist[i] = value

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

replace_nth(a, 3, 0)
print(a)  # -> [[0, 2, 3, 0, 5, 6], [0, 8, 9, 0, 11, 12], [0, 14, 15, 0, 17, 18]]

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

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