Scott wrote:
I am new to Python but I have studied hard and written a fairly big
(to me) script/program. I have solved all of my problems by Googling
but this one has got me stumped.

I want to check a string for a substring and if it exists I want to
create a new, empty list using that substring as the name of the list.
For example:

Let's say file1 has line1 through line100 as the first word in each
line.

for X in open("file1"):
    Do a test.
    If true:
        Y = re.split(" ", X)
        Z = Y[0]          # This is a string, maybe it is "Line42"
        Z = []              # This doesn't work, I want a new, empty
list created called Line42 not Z.

Is there any way to do this?

Assuming you made this work, and had a new variable called "Line42", how would you know it was called "Line42" in the rest of your program?

What you could do is create a dict and have the key set to the new name, e.g.:

new_names = {}
for X in open("file1");
    Do a test.
    if True:
        Y = X.split(" ")
        new_names[Y[0]] = []

then in the rest of your program you can refer to the keys in new_names:

for var in new_names:
    item = new_names[var]
    do_something_with(item)

Hope this helps!

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to