Home > Enterprise >  how to make a nested list to a matrix without using numpy and bracket
how to make a nested list to a matrix without using numpy and bracket

Time:04-20

a=[10*['-']]*10
print(a)
[['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],..]

how to make the "a" into a matrix without bracket and, finally turn something like this:

- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -

I want to use the join function!!

CodePudding user response:

You can use two calls to .join() -- one to handle space separating the dashes, and one to handle printing the grid out on multiple lines:

print('\n'.join(' '.join('-' * 10) for _ in range(10)))

This outputs:

- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
  • Related