2

I am making a program that reads a spread sheet. For each column, my program creates a list of all the values in each row of that column. To decide how many lists I need, I have the variable columnBound which is the total amount of columns in the spread sheet. How can I make a program that will sometimes create 3 lists if there are 3 columns and will sometimes create 8 if there are 8 columns?

If there were always 3 columns, for example, I know I could easily have list1, list2, list3, and build them as needed, but how can I have my program build a dynamic number of lists based on columnBound?

It's like I want

for x in range (0, columnBound):
    listx = [] 

Where I would have list1, list2, .... all the way to listx (or listcolumnBound)

I am very new to programming and would love conceptual help, a point in the right direction where. I don't exactly know how to google this question because it is very abstract.

Thanks!

Extra Info:

My program will use the spreadsheet as an input. Each column contains 5 digit reference numbers that correspond to a specific business address. Then, it will take a different spreadsheet where each row has a reference code but needs an address inserted into the last column. I will query each list to see if it has the matching ref code and enter in the respective address into the spreadsheet. Sometimes I will have 5 address columns, sometimes I might have 8. I know that making a program that is explicitly typed (where I specifically create list 1-8 and if there were 9 address columns, the 9th would be left out) is bad practice. I want to learn how to make my program adapt to how many columns there are.

Grant G
  • 49
  • 1
  • 4
  • 2
    Using a list of lists may be better here. – Sukrit Kalra Jul 16 '13 at 19:20
  • possible duplicate of [dynamic variable](http://stackoverflow.com/questions/10963804/dynamic-variable) – abarnert Jul 16 '13 at 19:28
  • I find it refreshing that I tried to do this when I was a beginner and I keep seeing questions of new people trying to programatically modify variable names – Stephan Jul 16 '13 at 19:29
  • 3
    This question has been asked many times. See all the Related questions on the right. Or [this blog post](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – abarnert Jul 16 '13 at 19:29
  • Or [this blog post](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html), which is much more concise—the title pretty much has 90% of the information you usually need. – abarnert Jul 16 '13 at 19:38

6 Answers6

1

You can use a list of lists:

Eg:

[['col1','col2'],[1,2]]

This way, you can have a dynamic number of lists.

jh314
  • 27,144
  • 16
  • 62
  • 82
1

You should use a list of list, or a dict of list.

hivert
  • 10,579
  • 3
  • 31
  • 56
1

A list of lists (or in fact a generator giving you tuples in turn) is the data type you would receive from the csv module. Which is probably what you want to use.

See: http://docs.python.org/dev/library/csv

Marcin
  • 48,559
  • 18
  • 128
  • 201
0

Use a list of lists to accomplish this. This line:

list = [[0]*3]*3 creates a list with three references to the single list [0,0,0].

Insert Text Here
  • 301
  • 1
  • 3
  • 5
  • First, it's a bad idea to call a list `list`. Second, now you have a list containing three references to the *same* list. If you do `list[0][0] = 1`, then your list has become `[[1, 0, 0], [1, 0, 0], [1, 0, 0]]`. – Tim Pietzcker Jul 16 '13 at 20:16
  • Thanks for pointing this out. I know that naming a list LIST is a bad idea, however, it is clear that this is a list. I wasn't aware of such a problem with my code. Didn't know that you could do such a thing. Thanks – Insert Text Here Jul 16 '13 at 20:37
0

You can create list of lists.You can use csv to extract each row.

    rows=list()
    for x in range(0, columnBound):
       rows.append(extracted_rows_in_each_column)

output will look like: rows=[[values of rows col #1],[values of rows col #2],.......]

 rows=[[values of rows col #1],[values of rows col #2],.......]
arkilic
  • 1
  • 1
0

In your case, I think it is best to use my library pyexcel which will read the excel file for you and give you the data in uniform data matrix. You can also add custom formatting to the data matrix. Then use it as if you had a two dimensional array.

Suppose you have these data in an excel file

1   2   3
4   5   6
7   8   9

Here is the example code showing how you can randomly access a cell:

>>> import pyexcel
>>> reader = pyexcel.Reader("example.xls")
>>> print reader[1][1]
5

Here is the tutoral on its usage

chfw
  • 4,502
  • 2
  • 29
  • 32