Creating a list of lists with sequential numbers

I am looking for a convenient way to create a list of lists for which lists in the list have sequential numbers. So far, I have only come up with a very unsatisfactory solution for rough printing (yes, I just use python for several weeks):

block0 = []
...
block4 = []

blocks = [block0,block1,block2,block3,block4]

I appreciate any help that works with something like nrBlocks = 5.

+3
source share
2 answers

It is not clear what serial numbers you are talking about, but your code translates to the following idiomatic Python:

[[] for _ in range(4)]          # use xrange in python-2.x
+5
source

Do not do it like this. Put it in blocksthe first place:

blocks = [
  [ ... ],
  [ ... ],
  [ ... ],
  [ ... ]
]
0
source

Source: https://habr.com/ru/post/1736090/


All Articles