Python Forum

Full Version: How search in multidim. list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there.

I have questinon. I need to search in my multidimensional list by more than one key. There is list:
lis= [["id","type","city","price","rooms"],[0,"Flat","Prague",13000,5],[1,"Flat","Prague",1000,4],[2,"House","Prague",13020,10]]
For example, user enter to console this: search [key] [value] [key2] [value2][where/order]. -> search city Prague AND type Flat ORDER BY price. This can show list by id : 1,0.

I know how order items , thats ok. But i dont know, how to search first by city and then by type and order it.

Can you everybody help me?
>>> lis= [["id","type","city","price","rooms"],[0,"Flat","Prague",13000,5],[1,"Flat","Prague",1000,4],[2,"House","Prague",13020,10]]
>>> sorted([item for item in lis if item[1]=='Flat' and item[2]=='Prague'], key=lambda x: x[3])
[[1, 'Flat', 'Prague', 1000, 4], [0, 'Flat', 'Prague', 13000, 5]]
however this looks like list of lists created by reading entire csv file in memory. is that the case? You may want to use dict reader. it will make the code more readble