Iterate over 2d array in an expanding circular spiral

前端 未结 7 605
自闭症患者
自闭症患者 2020-12-23 14:39

Given an n by n matrix M, at row i and column j, I\'d like to iterate over all the neighboring values in a c

7条回答
  •  萌比男神i
    2020-12-23 15:13

    Although I'm not entirely sure what you are trying to do, I'd start like this:

    def xxx():
        for row in M[i-R:i+R+1]:
            for val in row[j-R:j+r+1]:
                yield val
    

    I'm not sure how much order you want for your spiral, is it important at all? does it have to be in increasing R order? or perhaps clockwise starting at particular azimuth?

    What is the distance measure for R, manhattan? euclidean? something else?

提交回复
热议问题