Reputation: 49
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.
Upvotes: 2
Views: 5665
Reputation: 4592
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
Upvotes: 0
Reputation: 1
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],.......]
Upvotes: 0
Reputation: 49816
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
Upvotes: 1
Reputation: 331
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].
Upvotes: 0
Reputation: 27802
You can use a list of lists:
Eg:
[['col1','col2'],[1,2]]
This way, you can have a dynamic number of lists.
Upvotes: 1