Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try to avoid complex nested data structures. I believe people tend to grok them only while they are intensively using the data structure. After the program is finished, or is set aside for a while, the data structure quickly becomes mystifying.</p> <p>Objects can be used to retain or even add richness to the data structure in a saner, more organized way. For instance, it appears the <code>item</code> and <code>price</code> always go together. So the two pieces of data might as well be paired in an object:</p> <pre><code>class Item(object): def __init__(self,name,price): self.name=name self.price=price </code></pre> <p>Similarly, a person seems to have an <code>id</code> and <code>name</code> and a set of possessions:</p> <pre><code>class Person(object): def __init__(self,id,name,*items): self.id=id self.name=name self.items=set(items) </code></pre> <p>If you buy into the idea of using classes like these, then your <code>list_dicts</code> could become</p> <pre><code>list_people = [ Person('001','jim',Item('pencil',0.99)), Person('002','mary',Item('book',15.49)), Person('002','mary',Item('tape',7.99)), Person('003','john',Item('pen',3.49)), Person('003','john',Item('stapler',9.49)), Person('003','john',Item('scissors',12.99)), ] </code></pre> <p>Then, to merge the people based on <code>id</code>, you could use Python's <code>reduce</code> function, along with <code>take_items</code>, which takes (merges) the items from one person and gives them to another:</p> <pre><code>def take_items(person,other): ''' person takes other's items. Note however, that although person may be altered, other remains the same -- other does not lose its items. ''' person.items.update(other.items) return person </code></pre> <p>Putting it all together:</p> <pre><code>import itertools import operator class Item(object): def __init__(self,name,price): self.name=name self.price=price def __str__(self): return '{0} {1}'.format(self.name,self.price) class Person(object): def __init__(self,id,name,*items): self.id=id self.name=name self.items=set(items) def __str__(self): return '{0} {1}: {2}'.format(self.id,self.name,map(str,self.items)) list_people = [ Person('001','jim',Item('pencil',0.99)), Person('002','mary',Item('book',15.49)), Person('002','mary',Item('tape',7.99)), Person('003','john',Item('pen',3.49)), Person('003','john',Item('stapler',9.49)), Person('003','john',Item('scissors',12.99)), ] def take_items(person,other): ''' person takes other's items. Note however, that although person may be altered, other remains the same -- other does not lose its items. ''' person.items.update(other.items) return person list_people2 = [reduce(take_items,g) for k,g in itertools.groupby(list_people, lambda person: person.id)] for person in list_people2: print(person) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload