It's the classic trap we fall into when learing object-oriented 
programming.  The dictionary is a single INSTANCE of a dict.  It's like an 
individual and you could give it a name.  Let's say its name is David.  Now 
you load David up with a bunch of data and put him in a list.  

Now instead of making another dictionary, say Bob, you decide to load David 
up with a bunch more data and add him to the list AGAIN.  Wait.... how is 
that possible?  It just is.  David is always David.  You can have the 
variable "D" point to David, you can add him to a list, you can have a 
second variable "J" point to him too.  They're all pointing to exactly the 
same dictionary David.  Anything David picks up or drops is the same 
everywhere.

You just added David to the same list a bunch of times.

On Thursday, September 4, 2014 10:03:35 PM UTC-7, trr wrote:
>
> Thanks for the solution, Joe, Leonel, Stefaan. To continue, why should I 
> create a new dictionary. My idea was to use one dictionary and populate it 
> with all data. Will that not be more efficient? Thank you once again.
>
> On Wednesday, September 3, 2014 5:15:40 PM UTC+5:30, trr wrote:
>>
>> In controller I create a list of dictionaries from an uploaded excel 
>> file. Then I populate the list to a html table. I use xlrd lib for reading 
>> excel file. The problem is the dictionary gets correctly populated from 
>> excel but when I append the dictionary to the list and enumerate the list 
>> it shows the last appended dictionary values for all the list items. Here 
>> is my code. Pl. help. In python shell when i build a dictionary and append 
>> it to list and then enumerate the list it is listed correctly.
>>     d = {}
>>     l = []
>>     up = os.path.join(request.folder,'uploads')
>>     workbook = xlrd.open_workbook(os.path.join(request.folder,'uploads',
>> 'meas.xls'))
>>     worksheet = workbook.sheet_by_name('Sheet1')
>>     num_rows = worksheet.nrows - 1
>>     num_cells = worksheet.ncols - 1
>>     curr_row = -1
>>     while curr_row < num_rows:
>>         curr_row += 1
>>         row = worksheet.row(curr_row)
>>         #print 'Row:', curr_row
>>         curr_cell = -1
>>         while curr_cell < num_cells:
>>             curr_cell += 1
>>             # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 
>> 5=Error, 6=Blank
>>             cell_type = worksheet.cell_type(curr_row, curr_cell)
>>             cell_value = worksheet.cell_value(curr_row, curr_cell)
>>             #print '    ', cell_type, ':', cell_value
>>             if curr_cell == 0:
>>                 d['loc_of_work'] = cell_value
>>             if curr_cell == 1:
>>                 d['n'] = cell_value
>>             if curr_cell == 2:
>>                 d['t'] = cell_value
>>             if curr_cell == 3:
>>                 d['l'] = cell_value
>>             if curr_cell == 4:
>>                 d['b'] = cell_value
>>             if curr_cell == 5:
>>                 d['d'] = cell_value
>>         print 'dict'
>>         print d.items()
>>         l.append(d)
>>         print 'len of list:'
>>         print len(l)
>>         print 'list:'
>>         for i,j in enumerate(l):
>>             print i,j
>>
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to