vnikonov_63
vnikonov_63

Reputation: 191

Increasing the speed of reading a matrix using an array of array

I am trying to read a matrix in python, using the following code:

rows = int(input())
columns = int(input())

final = [''] * rows
for i in range(len(final)):
    final[i] = list(input())

This is giving me a time limit in the problem, are there faster ways to read a matrix and store it in a form of list of lists in python.

The input is

3
3
abc
def
ghi

I need to store this as

[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

Maybe there is a way to do in something like stdin, but I have no idea what that is

Upvotes: 0

Views: 65

Answers (1)

Ali Hassan
Ali Hassan

Reputation: 966

Try Numpy package, it is faster than the python list. The main difference is about the performance. Numpy data structures perform better in:

  • Size: Numpy data structures take less space than the list.
  • Performance: They have a need for speed and are faster than lists.
  • Functionality: Numpy and Scipy have optimized functions such as linear algebra operations.

For further information Try this link

Upvotes: 1

Related Questions