Re: How to manipulate list of dictionary



Simon Brunning a écrit :
2008/8/26 ajak_yahoo <arazak73@xxxxxxxxxxxx>:
Need some help, I have a list of dictionary as below,

table = [{"Part #":"Washer","Po #":"AE00128","qty":100},
{"Part #":"Brake Pad","Po #":"AE00154","qty":150},
{"Part #":"Mesh","Po #":"AE00025","qty":320},
{"Part #":"Mouse","Po #":"AE00207","qty":120},
{"Part #":"Insulator","Po #":"AE0013","qty":190}]

How to manipulate the table?

I need to search for the Po #, and display the result as below.


Part # : Mouse
Po # : AE00207
Qty : 120 pcs

Well, that's a really bad data structure for what you want to do, but
you can do it with something like (untested):

wanted = 'AE00207'

for part in table:
if part['Po #'] == wanted:
print "Part #:\t%(Part #)s\nPo #:\t%(Po #)s\nQty #:\t%(qty)s" % part


Which will not be very efficient if you happen to have lot of items in your list and or a lot of searches to do.


The next solution is to maintain an index, ie:

def make_index(table, key):
index = {}
for record in enumerate(table):
index.setdefault(record[key], []).append(record)
return index

po_index = make_index(table, "Po #")
results = po_index.get('AE00207', None)


But this won't scale up if you have millions of records. So the next next solution is to use a true database - either a RDBMS, or an embedded one like SQLite.

.



Relevant Pages

  • Re: editing current record
    ... Penny George wrote: ... It queries a database to display this information. ... >when I try to add a new record and manipulate the data in a drop down box. ... There is only one set of properties for a form control. ...
    (microsoft.public.access.formscoding)
  • Re: collection to dataset
    ... > collection class. ... > so i can "manipulate" the data and display it. ... "Programming is an art form that fights back" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: table-cell
    ... why don't you simply manipulate the document tree? ... display: table-cell; ... However, if you really want to make yourself dependent on CSS as well as scripting, why don't you use just the first one of the rules and dynamically change the class property of a cell or row to "some_row_class" or to something else, depending on whether you want it to be displayed or not? ...
    (comp.infosystems.www.authoring.stylesheets)
  • Visual FoxPro as a frontend for Firebird Open Source DBMS?
    ... in any way to access / manipulate / edit / display or report on data ...
    (microsoft.public.fox.programmer.exchange)