473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: How to manipulate list of dictionary

2008/8/26 ajak_yahoo <ar******@yahoo.com.my>:
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

--
Cheers,
Simon B.
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns
Aug 26 '08 #1
1 1477
Simon Brunning a écrit :
2008/8/26 ajak_yahoo <ar******@yahoo.com.my>:
>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.

Aug 26 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
3
by: erik gartz | last post by:
Hello, I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain , ] instead. Doesn't {} allocate new memory for the dictionary each time?...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
12
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have a dictionary like this: Dictionary<List<string>, List<string>> How can I iterate in it? Thanks very much. -- Luigi
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.