简体   繁体   English

如何制作所有列和行的坐标列表

[英]How to make a list of the coordinates of all columns and rows

Suppose I have a nxn grid, and I want a function that generates a list of all columns and rows by taking n as the input, in python.假设我有一个nxn网格,并且我想要一个 function,它通过在 python 中以n作为输入来生成所有列和行的列表。 By a list of columns, I mean each column is represented as its own list, with elements being coordinates of the elements in the column.通过列列表,我的意思是每列都表示为自己的列表,元素是列中元素的坐标。 (Or each column could be a set of coordinates, that would work too) (或者每列可以是一组坐标,这也可以)

I could do this using two list comprehensions,我可以使用两个列表推导来做到这一点,

x = [[ (i, j) for j in range(n)] for i in range(n)] + [[ (i, j) for i in range(n)] for j in range(n)]

With n=3 this produces a list with 9 elements, each of which is a list of 3 coordinates.n=3时,这会产生一个包含 9 个元素的列表,每个元素都是 3 个坐标的列表。

x = [[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]

I was wondering if there is a cleaner way to do the same thing, maybe using itertools or a similar module.我想知道是否有更清洁的方法来做同样的事情,也许使用 itertools 或类似的模块。

Not exactly sure if this is what you're looking for but you could try itertools.product不完全确定这是否是您正在寻找的,但您可以尝试itertools.product
For example例如

>>> from itertools import product
>>> n = 3

>>> list(product(range(n),range(n)))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

>>> [list(product(range(n),range(n)))[n*i:n*i+n] for i in range(n)]
[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]]

>>> [list(product(range(n),range(n)))[i::n] for i in range(n)]
[[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]

暂无
暂无

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

相关问题 如何将列表中的前三个元素作为列,其余作为行 - Python - How to make first three elements in a list as columns and the rest as rows - Python 如何在Python中将嵌套列表分成数据框中的行和列 - How to make nested list into rows and columns in a data frame in Python 如何制作一个简单的 Pandas DataFrame 等值线图,其中数字单元格值为 Z,行/列标记为 X 和 Y 坐标? - How do I make a make a simple contour chart of a Pandas DataFrame with numeric cell values as Z and labeled rows/columns as X and Y coordinates? 如何让matplotlib显示所有x坐标? - How to make matplotlib show all x coordinates? 如何收集第一个坐标并合并列表列表的所有坐标? - How to gather the first coordinates and merge all coordinates of a list of lists? 将数据框划分为包含所有列的行列表 - Divide dataframe into list of rows containing all columns 如何获取 2 列中的列表并创建一个元组,将所有元组放入一个列表中,然后返回该列表? - How do I take a list that is in 2 columns and make a tuple, put all the tuples in a list, and return that list? 对于循环列出列表中每一列的所有行数据 - pandas - For loop list all rows data by each columns in list - pandas 在任何列列表中查找包含所有字符串列表的数据框行 - Find dataframe rows with all of list of strings in any of list of columns 我将如何检查和操作二维列表的所有行和列中的匹配值? - How would I check for and manipulate matching value within all rows and columns of 2d list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM