Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何操作列表列表?_Python_Nested Lists - Fatal编程技术网

Python 如何操作列表列表?

Python 如何操作列表列表?,python,nested-lists,Python,Nested Lists,如何遍历列表列表,使任何带有“1”的列表的顶部(0)、左上(0)、右上(0)、底部(0)、右下(0)、左下(0)也成为“1”,如下所示?使列表1变为列表2 list_1 =[[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0]] list_2 =[[0,0,0,0,0,0,0,0], [0,0,1,1,1,0,0,0],

如何遍历列表列表,使任何带有“1”的列表的顶部(0)、左上(0)、右上(0)、底部(0)、右下(0)、左下(0)也成为“1”,如下所示?使列表1变为列表2

list_1 =[[0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0],
         [0,0,0,1,0,0,0,0],
         [0,0,0,0,0,0,0,0]]


list_2 =[[0,0,0,0,0,0,0,0],
         [0,0,1,1,1,0,0,0],
         [0,0,1,1,1,0,0,0],
         [0,0,1,1,1,0,0,0]]

这是图像处理中常见的一种称为“膨胀”的操作。您的问题是二维的,因此最好使用

  • 比列表列表更合适的二维数据结构,以及
  • 一个已经可用的库函数,而不是重新发明轮子
下面是一个分别使用numpy
ndarray
和scipy的
二进制扩容的示例:

>>> import numpy as np
>>> from scipy import ndimage
>>> a = np.array([[0,0,0,0,0,0,0,0],
                  [0,0,0,0,0,0,0,0],
                  [0,0,0,1,0,0,0,0],
                  [0,0,0,0,0,0,0,0]], dtype=int)
>>> ndimage.binary_dilation(a, structure=ndimage.generate_binary_structure(2, 2)).astype(a.dtype)
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0]])

使用numpy,通常更适合于操作2D列表。如果您正在进行图像分析,请参阅@wim answer。否则,这里是你如何管理它与numpy只

> import numpy as np
> list_1 =[[0,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0],
           [0,0,0,1,0,0,0,0],
           [0,0,0,0,0,0,0,0]]
> l = np.array(list_1) # convert the list into a numpy array
> pos = np.where(l==1) # get the position where the array is equal to one
> pos
(array([2]), array([3])) 

# make a lambda function to limit the lower indexes:
get_low = lambda x: x-1 if x>0 else x
# get_high is not needed.

# slice the array around that position and set the value to one
> l[get_low(pos[0]):pos[0]+2,  
    get_low(pos[1]):pos[1]+2] = 1

> l
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0, 0]])

> corner
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1]])

> p = np.where(corner==1)
> corner[get_low(p[0]):p[0]+2,  
         get_low(p[1]):p[1]+2] = 1
> corner
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 1, 1]])

HTH

非常感谢:)我非常感谢@jeanrjc的帮助谢谢,这段代码正是我所需要的。我如何将阵列恢复到列表中?(不带括号())@MrAutodidact:
corner.tolist()
@jeanrjc谢谢…我一直很感激你的帮助谢谢你的帮助和信息丰富的回复:)我会研究一下扩张