如何为二维列表编号? [英] How to number a 2 dimensional list?
本文介绍了如何为二维列表编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将这些列表包含在我要编号的列表中.我也有两个变量,行和列,它们被当作输入.
I have these lists inside a list that I am trying to number. I also have two variables, row and column, which are taken as input.
lst = row*[column*[0]]
这将使列表内的列表充满零,因此,如果row = 6和column = 10,则看起来像这样:
This makes lists inside a list that are filled with zeros, so if row = 6 and column = 10, it would look like:
[[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, 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]]
如何更改它,使其看起来像这样:
How can I change it so it looks like this:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
[51, 52, 53, 54, 55, 56, 57, 58, 59, 60]]
谢谢!
推荐答案
我认为这将是最"pythonic"的方式:
I think this would be the most "pythonic" way:
from pprint import pprint
rows = 6
cols = 10
lst = [[(j*cols+i) for i in range(1,cols+1)] for j in range(rows)]
pprint(lst)
输出:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
[51, 52, 53, 54, 55, 56, 57, 58, 59, 60]]
这篇关于如何为二维列表编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文