Posts: 279
Threads: 107
Joined: Aug 2019
For this program, the input will be table data and the output will be a table.
In order to solicit data, I'm asking how many rows and then iterating to get the data for each row.
Can I use iteration (i.e. a for loop) to create variable names themselves? What I actually want are names of the lists that will correspond to the rows.
Thanks!
Posts: 8,154
Threads: 160
Joined: Sep 2016
Oct-16-2019, 03:33 PM
(This post was last modified: Oct-16-2019, 03:33 PM by buran.)
(Oct-16-2019, 03:29 PM)Mark17 Wrote: Can I use iteration (i.e. a for loop) to create variable names themselves? What I actually want are names of the lists that will correspond to the rows.
Although possible, creating variable names dynamically is really bad idea
use proper data structure like dict or list
https://python-forum.io/Thread-dynamical...7#pid91757
Posts: 1,358
Threads: 2
Joined: May 2019
What you likely want is a list of lists. Nested for loops will do this nicely.
num_rows = int(input("Rows: "))
num_cols = int(input("Cols: "))
for row in range(num_rows):
for col in range(num_cols):
#Here is where you get the data for each row and col
#new_data is the row you are creating from the input data
new_data.append(whatever you get as a cell of input)
#Once you assemble a row's worth of data, then
table = table.append(new_data)
print(table)
#To access a single cell, access by row and column
single_cell = table[row][col] Designed this as a format - it obviously does not work as written, but is meant to spur you in a direction
Posts: 4,220
Threads: 97
Joined: Sep 2016
If you are making a data table, you may also want to look at pandas, specifically DataFrames.
Posts: 279
Threads: 107
Joined: Aug 2019
Oct-16-2019, 04:37 PM
(This post was last modified: Oct-16-2019, 04:40 PM by Mark17.)
(Oct-16-2019, 03:52 PM)jefsummers Wrote: What you likely want is a list of lists. Nested for loops will do this nicely.
num_rows = int(input("Rows: "))
num_cols = int(input("Cols: "))
for row in range(num_rows):
for col in range(num_cols):
#Here is where you get the data for each row and col
#new_data is the row you are creating from the input data
new_data.append(whatever you get as a cell of input)
#Once you assemble a row's worth of data, then
table = table.append(new_data)
print(table)
#To access a single cell, access by row and column
single_cell = table[row][col] Designed this as a format - it obviously does not work as written, but is meant to spur you in a direction
I've been learning this approach with regard to iterating over rows/columns to fill arrays in VBA. Python doesn't support arrays, though, so I think the main source of confusion for me is still where I would be storing that new data. What is new_data? If it's a list then the program needs to be able to create n lists when the user says there will be n rows (for example).
(Oct-16-2019, 03:33 PM)buran Wrote: (Oct-16-2019, 03:29 PM)Mark17 Wrote: Can I use iteration (i.e. a for loop) to create variable names themselves? What I actually want are names of the lists that will correspond to the rows.
Although possible, creating variable names dynamically is really bad idea
use proper data structure like dict or list
https://python-forum.io/Thread-dynamical...7#pid91757
I went through Ned Batchelder's blog post on this (mentioned in the link you provided). I don't really understand it. I hear what he's saying (kind of) about going up a level in the data modeling. I don't understand why. Seeing a simple example and the kind of traceback that can result might help me understand a bit more.
Posts: 1,358
Threads: 2
Joined: May 2019
Exactly. new_data is a list that you create by appending each item as read in from the user. You then append new_data to the table.
Oh, and numpy and pandas support arrays, but don't think you are there yet.
Posts: 279
Threads: 107
Joined: Aug 2019
(Oct-16-2019, 04:43 PM)jefsummers Wrote: Exactly. new_data is a list that you create by appending each item as read in from the user. You then append new_data to the table.
Oh, and numpy and pandas support arrays, but don't think you are there yet.
What data structure is table?
I looked up how to do tables in Python and found a method (?) using PrettyTable. For this exercise, I'm just looking to plug into that formula.
Posts: 1,358
Threads: 2
Joined: May 2019
Oct-16-2019, 10:43 PM
(This post was last modified: Oct-16-2019, 10:53 PM by jefsummers.)
table is a list. Each of the entries in that list is a list. So, as a list of lists it is a table/grid.
Here is another example of the technique. I wrote this as an exercise, Conway's Game of Life. I needed a grid of buttons. Here's the code I used to create them (I use Wx as my GUI)
def screen_setup(size, frame) :
for row in range(size) :
button_row = []
for col in range(size) :
btn = wx.ToggleButton(frame, label='', size = (cell_size,cell_size), pos=(cell_size*col, cell_size*row))
button_row.append(btn)
button_grid.append(button_row)
button_row.clear Then I read a cell using def read_cell(row,col) :
if button_grid[row][col].GetValue() :
return 1
else:
return 0 So, you access the grid as button_grid[row][col]
Clear?
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Oct-16-2019, 04:37 PM)Mark17 Wrote: I don't understand why. Seeing a simple example and the kind of traceback that can result might help me understand a bit more.
Simple example. Lets say we have class and want have ledger. Normal way would be to have dictionary but we can to it with separate variables:
john = 'student'
mary = 'student'
sonia = 'teacher' How could we find out how many students we have? How could we find out names of the students? etc, etc
On the other hand, with dictonaries it's simple:
>>> class_ledger = {'john': 'student', 'maria': 'student', 'sonia': 'teacher'}
>>> len(class_ledger) # total number of people
3
>>> sum(1 for k, v in class_ledger.items() if v == 'student') # number of students
2
>>> print(*(k for k, v in class_ledger.items() if v == 'student'), sep='\n') # names of students
john
maria
>>> 'maria' in class_ledger # is maria in a ledger
True
>>> class_ledger.update({'Diego': 'student'}) # add new person to class
>>> class_ledger
{'john': 'student', 'maria': 'student', 'sonia': 'teacher', 'Diego': 'student'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|