Probably somewhat silly newbie question

E

elgrandchignon

Hi all--

Trying to learn Python w/little more than hobbyist (bordering on pro/
am, I guess) Perl as a background.

My problem is, I have a list of departments, in this instance, things
like "Cheese", "Bakery", et al. (I work @ a co-op health food store).
I've populated a list, 'depts', w/these, so that their indexes match
our internal indexing (which skips a few #'s).

Now, I'd like to simply generate-- and be able to refer to-- a bunch
of other lists-sets (for subdepartments) by iterating through this
list, and naming each of these subdepartment lists "categoryx", where
x is the index # from the master 'depts' list. And then be able to
populate & refer to these lists by accessing their variable-including
name.

In Perl, it's a fairly trivial matter to use a string variable in
naming some other kind of variable-- not sure about Python though. My
initial, very weak stab at it (don't laugh!) went something like this:

for i in range(len(depts)):
if depts:
categorylistdeptname = 'category' + str(i)
categorylistdeptname = []

Not sure what that wound up doing, but it sure didn't seem to work.

I'm mean, it's trivial enough to just set up the lists manually;
'category1 = []', 'category2 = []' -- but then, when I'm cycling
through the item file-- which contains one field that's a list of
subdepartments for the item, & another field w/the item's dept #, I
would like to be able to just append new subdept's into whichever
categorylist is indicated by the files department #.

Hope that's sorta clear. Maybe I'm just SOL, but I thought I'd try
asking around before I go crawling back to Perl w/my tail tucked
between my legs....
 
J

James Stroud

Hi all--

Trying to learn Python w/little more than hobbyist (bordering on pro/
am, I guess) Perl as a background.

My problem is, I have a list of departments, in this instance, things
like "Cheese", "Bakery", et al. (I work @ a co-op health food store).
I've populated a list, 'depts', w/these, so that their indexes match
our internal indexing (which skips a few #'s).

Now, I'd like to simply generate-- and be able to refer to-- a bunch
of other lists-sets (for subdepartments) by iterating through this
list, and naming each of these subdepartment lists "categoryx", where
x is the index # from the master 'depts' list. And then be able to
populate & refer to these lists by accessing their variable-including
name.

In Perl, it's a fairly trivial matter to use a string variable in
naming some other kind of variable-- not sure about Python though. My
initial, very weak stab at it (don't laugh!) went something like this:

for i in range(len(depts)):
if depts:
categorylistdeptname = 'category' + str(i)
categorylistdeptname = []

Not sure what that wound up doing, but it sure didn't seem to work.


First, your are rebinding categorylistdeptname in the loop every time.

But you probably want a dict (in python 2.4 or later):


deptdict = dict((dept, []) for dept in depts))

And this gets what you want, believe it or not.

Now you can populate each list:

deptdict['Bakery'].append("Donuts")
deptdict['Bulk'].extend(["Granola", "Rasins"])

And work witht the lists by name:

for item in deptdict['Bulk']:
print item
# prints "Granola", "Rasins", etc.


James
 
J

James Stroud

James said:
Hi all--

Trying to learn Python w/little more than hobbyist (bordering on pro/
am, I guess) Perl as a background.

My problem is, I have a list of departments, in this instance, things
like "Cheese", "Bakery", et al. (I work @ a co-op health food store).
I've populated a list, 'depts', w/these, so that their indexes match
our internal indexing (which skips a few #'s).

Now, I'd like to simply generate-- and be able to refer to-- a bunch
of other lists-sets (for subdepartments) by iterating through this
list, and naming each of these subdepartment lists "categoryx", where
x is the index # from the master 'depts' list. And then be able to
populate & refer to these lists by accessing their variable-including
name.

In Perl, it's a fairly trivial matter to use a string variable in
naming some other kind of variable-- not sure about Python though. My
initial, very weak stab at it (don't laugh!) went something like this:

for i in range(len(depts)):
if depts:
categorylistdeptname = 'category' + str(i)
categorylistdeptname = []

Not sure what that wound up doing, but it sure didn't seem to work.



First, your are rebinding categorylistdeptname in the loop every time.

But you probably want a dict (in python 2.4 or later):


deptdict = dict((dept, []) for dept in depts))

And this gets what you want, believe it or not.

Now you can populate each list:

deptdict['Bakery'].append("Donuts")
deptdict['Bulk'].extend(["Granola", "Rasins"])

And work witht the lists by name:

for item in deptdict['Bulk']:
print item
# prints "Granola", "Rasins", etc.


James


Typo, too many parens. Should be:

deptdict = dict((dept, []) for dept in depts)
 
E

elgrandchignon

Trying to learn Python w/little more than hobbyist (bordering on pro/
am, I guess) Perl as a background.
My problem is, I have a list of departments, in this instance, things
like "Cheese", "Bakery", et al. (I work @ a co-op health food store).
I've populated a list, 'depts', w/these, so that their indexes match
our internal indexing (which skips a few #'s).
Now, I'd like to simply generate-- and be able to refer to-- a bunch
of other lists-sets (for subdepartments) by iterating through this
list, and naming each of these subdepartment lists "categoryx", where
x is the index # from the master 'depts' list. And then be able to
populate & refer to these lists by accessing their variable-including
name.
In Perl, it's a fairly trivial matter to use a string variable in
naming some other kind of variable-- not sure about Python though. My
initial, very weak stab at it (don't laugh!) went something like this:
for i in range(len(depts)):
if depts:
categorylistdeptname = 'category' + str(i)
categorylistdeptname = []

Not sure what that wound up doing, but it sure didn't seem to work.

First, your are rebinding categorylistdeptname in the loop every time.

But you probably want a dict (in python 2.4 or later):

deptdict = dict((dept, []) for dept in depts))

And this gets what you want, believe it or not.

Now you can populate each list:

deptdict['Bakery'].append("Donuts")
deptdict['Bulk'].extend(["Granola", "Rasins"])

And work witht the lists by name:

for item in deptdict['Bulk']:
print item
# prints "Granola", "Rasins", etc.

James


Thanks so much James! I'll give that method a shot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top