[Tutor] assignment to 1 item in dict assigns to all items in
dict?
Charlie Clark
charlie@begeistert.org
Fri Apr 11 10:38:10 2003
On 2003-04-11 at 14:59:10 [+0200], you wrote:
> I see that you're right, I want to create a new instance of line_item
> each time and add it into the dictionary grossinc instead of making a
> reference, but I'm still confused about a good way to do that. Make a
> class? My code should have read:
>
> # line item = [list of transactions (dict), associations (list), goal
> (int)]
> line_item = [{'Date': ()}, [], 0]
>
> # budget = {dictionary of line_items}
> grossinc = {}
>
> #define budget line items
> grossinc['salary'] = line_item
> grossinc['interest'] = line_item
> grossinc['dividends'] = line_item
> grossinc['other'] = line_item
>
> grossinc['salary'][2] = 3333
> grossinc['other'][2] = 575
A dictionary or list of dictionaries will probably be fine. I'd like to
help you set this up but I need to see how you create a line_item
You might want something like this
items = []
or
items = {}
it really depends on what you want to do with your data. I think a list
might be what you want in this case.
you can then insert items/line_items into your structure, I think a
dictionary is the write thing for this.
ie.
item = {'transactions':'', 'associations':'', 'goal':0}
you can then do something like
new_item = item.copy()
new_item['transactions'] = ### I've have no idea what you really
....
items.append(new_item)
Try this with a few items and then look at the resulting list "items" to
see if it makes sense.
Charlie (non-Guru of the week)