user1020069
user1020069

Reputation: 1570

Multi dimensional arrays in Python of a dynamic size

very new to python so attempting to wrap my head around multi dimensional arrays. I read the existing posts and most of them deal with multi dimensional arrays given dimensions. In my case, I do not have dimensions for the total number of rows possible. A file is being processed, which is CSV and has 7 columns, but each line, depending on meeting or failing a criteria is accordingly drafted into an array. Essentially each line has 7 columns, but the number of rows cannot be predicted. The line is being treated as a list.

My aim is to create a multidimensional array of eligible lines and then be able to access values in the array. how can I do this?

essentially, how do I tackle creating a 2D list:

list_2d = [[foo for i in range(m)] for j in range(n)]

The above creates an mxn sized list but in my case, I know only n (columns) and not m(rows)

Upvotes: 14

Views: 74497

Answers (6)

Pete
Pete

Reputation: 31

I discovered this to create a simple 2D array list that is 8 elements wide and dynamic in the other dimension

list2d=[[] for i in xrange(8)]

Then you can assign any number of variables to the 8 wide array

list2d[0]=[1,2,3,4,5,6,7,8,9,10,11]
list2d[1]=[12,13,14,15,16,17,18,19]

and so on.....

Upvotes: 3

Nageswar Rao Mandru
Nageswar Rao Mandru

Reputation: 21

try below

#beg 

a=[[]]

r=int(input("how many rows "))
c=int(input("how many cols "))

for i in range(r-1):
    a.append([])

for i in range(r):
    print("Enter elements for row ",i+1)
    for j in range(c):
        num=int(input("Enter element "))
        a[i].append(num)

for i in range(len(a)):
     print()
     for j in range(len(a[i])):
         print(a[i][j],end="  ")

end

Upvotes: 2

Pranav Karmalkar
Pranav Karmalkar

Reputation: 21

You can even try this it worked for me

s = [[] for y in range(n)]

Upvotes: 2

Pablo Jomer
Pablo Jomer

Reputation: 10428

In python there is no need to declare list size on forehand.

an example of reading lines to a file could be this:

file_name = "/path/to/file"
list = []

with open(file_name) as file:
  file.readline
  if criteria:
    list.append(line)

For multidimensional lists. create the inner lists in a function on and return it to the append line. like so:

def returns_list(line):
  multi_dim_list = []
  #do stuff
  return multi_dim_list

exchange the last row in the first code with

list.append(returns_list(line))

Upvotes: 3

Jon Clements
Jon Clements

Reputation: 142226

If you're guaranteed 'n' columns, then you can transpose in memory.

from collections import defaultdict
import csv

cols = defaultdict(list)

with open('somefile.csv') as csvin:
    for row in csv.reader(csvin):
        for colno, col in enumerate(row):
            cols[colno].append(col)

Still not 100% sure this is your question though...

Upvotes: 0

VoronoiPotato
VoronoiPotato

Reputation: 3183

Nest lists in lists you don't need to predefine the length of a list to use it and you can append on to it. Want another dimension simply append another list to the inner most list.

[[[a1, a2, a3]  , [b1, b2, b3] , [c1, c2, c3]],
[[d1, d2, d3]  , [e1, e2, e3] , [f1, f2, f3]]]

and to use them easily just look at Nested List Comprehensions

Upvotes: 8

Related Questions