简体   繁体   English

计算二维列表 python 周围正方形中有多少个符号

[英]Calculating how many symbols in surrounding squares in 2d list python

I have an assignment in my intro to python course where I have to make a function that goes through a 2d list using loops and when given x and y coordinates it counts how many "N" are in the surrounding squares of the chosen one.我在 python 课程的介绍中有一个作业,我必须制作一个 function 使用循环遍历二维列表,当给定 x 和 y 坐标时,它会计算所选方格周围有多少“N”。 x and y can be anywhere and they are not allowed to cross the bounds of the list. x 和 y 可以在任何地方,并且不允许它们越过列表的边界。 For some reason the course material seems very lacking and I can't find anything that will help me get started.由于某种原因,课程材料似乎非常缺乏,我找不到任何可以帮助我入门的东西。 How should I go about making the function work?我应该如何 go 关于使 function 工作?

Paremeters for the function have to be (x, y, the list) The function should assume you are not on an "N" but if you are it counts that one aswell. function 的参数必须是(x,y,列表) function 应该假设您不在“N”上,但如果您是,它也算一个。

If someone can give me some tips on how to get started that would be great.如果有人可以给我一些关于如何开始的提示,那就太好了。

list = [['N', ' ', ' ', ' ', ' '],
        ['N', 'N', 'N', 'N', ' '],
        ['N', ' ', 'N', ' ', ' '],
        ['N', 'N', 'N', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' ']]

     

From this code you can count how many N in the list:从此代码中,您可以计算列表中有多少 N:

list_ = [['N', ' ', ' ', ' ', ' '],
        ['N', 'N', 'N', 'N', ' '],
        ['N', ' ', 'N', ' ', ' '],
        ['N', 'N', 'N', ' ', ' '],
        [' ', ' ', ' ', ' ', ' '],
        [' ', ' ', ' ', ' ', ' ']]
count = 0
for a in list_:
    for b in a:
        if b == 'N':
            count += 1
print(count)

From the question it is unclear whether the provided x and y coordinates will be an element around the edge with less than 8 adjacent elements or not.从问题中不清楚提供的 x 和 y 坐标是否是边缘周围的元素,相邻元素少于 8 个。

If the x coordinate had a range of 1 to len(row) - 1 and the y coordinate had a range of 1 to len(column)-1 then you could just loop through each row in the 2d list and then each element in that row and then check if that element is equal to 'N' .如果 x 坐标的范围为1len(row) - 1并且 y 坐标的范围为1len(column)-1那么您可以循环遍历 2d 列表中的每一行,然后遍历其中的每个元素行,然后检查该元素是否等于'N' This could be solved like so:这可以这样解决:

def adjacent_count(x, y, list_2d):
    count = 0
    for row in list_2d[x-1:x+2]:
        for element in row[y-1:y+2]:
            if element == 'N':
                count += 1
    return count

Or using list comprehension in one line:或者在一行中使用列表理解:

def adjacent_count(x, y, list_2d):
    return sum([1 if element == 'N' else 0 for row in list_2d[x-1:x+2] for element in row[y-1:y+2]])

EDIT: Knowing now that the x and y coordinates can have a range from 0 to len(rows) and len(cols) and that the function should not wrap to the other side of the array the following code can be used.编辑:现在知道 x 和 y 坐标的范围可以从0len(rows)len(cols)并且 function 不应该换行到数组的另一侧,可以使用以下代码。 The code now checks to see if the next array element is out of the bounds of the list before trying to access it.代码现在在尝试访问之前检查下一个数组元素是否超出列表的范围。

def adjacent_count(x, y, list_2d):
    no_rows, no_cols = len(list_2d), len(list_2d[0])
    count = 0
    for i in range(x-1, x+2):
        for j in range(y-1, y+2):
            if i < no_cols and j < no_rows:
                if list_2d[j][i] == 'N':
                    count += 1
    return count

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

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