It's not quite clear what you are asking. It looks like you are 
implementing the same looping as in Massimo's original example -- what was 
wrong with that code? Anyway, if you want to use an index in the loop, it 
would be range(len(persons)).

Anthony

On Wednesday, July 6, 2016 at 11:12:27 AM UTC-4, Ron Chatterjee wrote:
>
> I am not too familiar with python way of looping since I come from 
> traditional coding background. How would this for loop be implemented? in 
> terms of.
>
> persons = {'Max','Tim','Jim'};
> item = {'Chair','Table','Bike''};
>
> #nested for loop.
>
> for i in range of len(persons):
>         ii = db.person.insert(name=persons[i]);
>          for j in range of len(item):
>                 db.thing.insert(owner=ii, name=persons[i]+"'s "+item[j]);
>
> #no end needed for the loop to finish.
>
> rows = db(db.thing).select().join(db.person.id)
>
> print(rows.as_json())
> Am I correct?
>
>
>               
>       
>
> On Wednesday, July 6, 2016 at 7:24:53 AM UTC-4, Marlysson Silva wrote:
>>
>> Great funcionality!!
>>
>> Em terça-feira, 5 de julho de 2016 15:20:06 UTC-3, Massimo Di Pierro 
>> escreveu:
>>>
>>> db = DAL()
>>>
>>> db.define_table('person',Field('name'))
>>>
>>> db.define_table('thing',Field('name'),Field('owner','reference person'))
>>>
>>>
>>> for name in ('Max','Tim','Jim'):
>>>
>>>     i = db.person.insert(name=name)
>>>
>>>     for thing in ('Chair','Table','Bike'):
>>>
>>>         db.thing.insert(owner=i, name=name+"'s "+thing)
>>>
>>>
>>> rows = db(db.thing).select().join(db.person.id)
>>>
>>> print(rows.as_json())
>>>
>>>
>>> """                                                                     
>>>                                                                             
>>>                                        
>>>
>>> [{"owner": {"id": 1, "name": "Max"}, "id": 1, "name": "Max's Chair"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 1, "name": "Max"}, "id": 2, "name": "Max's Table"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 1, "name": "Max"}, "id": 3, "name": "Max's Bike"},    
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 2, "name": "Tim"}, "id": 4, "name": "Tim's Chair"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 2, "name": "Tim"}, "id": 5, "name": "Tim's Table"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 2, "name": "Tim"}, "id": 6, "name": "Tim's Bike"},    
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 3, "name": "Jim"}, "id": 7, "name": "Jim's Chair"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 3, "name": "Jim"}, "id": 8, "name": "Jim's Table"},   
>>>                                                                             
>>>                                        
>>>
>>>  {"owner": {"id": 3, "name": "Jim"}, "id": 9, "name": "Jim's Bike"}]    
>>>                                                                             
>>>                                        
>>>
>>> """
>>>
>>>
>>>
>>> rows = db(db.person).select().join(db.thing.owner, name="owns", fields=[
>>> db.thing.id, db.thing.name])
>>>
>>> print(rows.as_json())
>>>
>>>
>>> """                                                                     
>>>                                                                             
>>>                                        
>>>
>>> [{"id": 1, "name": "Max", "owns": [                                     
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 1, "name": "Max's Chair"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 2, "name": "Max's Table"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 3, "name": "Max's Bike"}]},                                  
>>>                                                                             
>>>                                        
>>>
>>>  {"id": 2, "name": "Tim", "owns": [                                     
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 4, "name": "Tim's Chair"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 5, "name": "Tim's Table"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 6, "name": "Tim's Bike"}]},                                  
>>>                                                                             
>>>                                        
>>>
>>>  {"id": 3, "name": "Jim", "owns": [                                     
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 7, "name": "Jim's Chair"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 8, "name": "Jim's Table"},                                   
>>>                                                                             
>>>                                        
>>>
>>>     {"id": 9, "name": "Jim's Bike"}]}                                   
>>>                                                                             
>>>                                        
>>>
>>> ]                                                                       
>>>                                                                             
>>>                                        
>>>
>>> """
>>>
>>>
>>> this is designed to be efficient and work on GAE too as long as rows is 
>>> not too long.
>>>
>>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to