Scott wrote:
Thank you fine folks for getting back with your answers!
So down the road I do dictname[line42].append("new stuff"). (or [var]
if I'm looping through the dict)
Nope, you still haven't gotten it. Of course, I really don't know where
you're going wrong, since you didn't use the same symbols as any of the
responses you had gotten.
I suspect that you meant dictname[] to be the dictionary that Duncan
called values[]. On that assumption, in order to append, you'd want
something like:
values["line42"].append("new stuff")
or
values[var].append("new stuff") if you happen to have a variable called
var with a value of "line42".
You will need to get a firm grasp on the distinctions between symbol
names, literals, and values. And although Python lets you blur these in
some pretty bizarre ways, you haven't a chance of understanding those
unless you learn how to play by the rules first. I'd suggest your first
goal should be to come up with better naming conventions. And when
asking questions here, try for more meaningful data than "Line42" to
make your point.
Suppose a text file called "customers.txt" has on each line a name and
some data. We want to initialize an (empty) list for each of those
customers, and refer to it by the customer's name. At first glance we
might seem to want to initialize a variable for each customer, but our
program doesn't know any of the names ahead of time, so it's much better
to have some form of collection. We choose a dictionary.
transactions = {}
with open("customers.txt") as infile:
for line in infile:
fields = line.split()
customername = fields[0] #customer is first thing on
the line
transactions[customername] = [] #this is where we'll put
the transactions at some later point, for this customer
Now, if our program happens to have a special case for a single
customer, we might have in our program something like:
transactions["mayor"].append("boots")
But more likely, we'll be in a loop, working through another file:
.....
for line in otherfile:
fields = line.split()
customername = fields[0]
transaction = fields[1]
transactions[customername].append(transaction) #append
one transaction
or interacting:
name = raw_input("Customer name")
trans = raw_input("transaction for that customer")
transactions[name].append(trans)
--
http://mail.python.org/mailman/listinfo/python-list