Python Forum
How search in multidim. list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How search in multidim. list
#1
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?
Reply
#2
>>> 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,266 Nov-03-2023, 05:35 PM
Last Post: huzzug
  search a list or tuple for a specific type ot class Skaperen 8 1,950 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Use one list as search key for another list with sublist of list jc4d 4 2,174 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Search in an unsorted list amir_0402 2 15,245 Jun-04-2020, 10:25 PM
Last Post: deanhystad
  Alpha numeric element list search rhubarbpieguy 1 1,795 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  search binary file and list all founded keyword offset Pyguys 4 2,787 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Using Python to search through a list of urls jeremy 4 2,877 Dec-18-2019, 11:52 AM
Last Post: Malt
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 6,736 May-29-2019, 02:27 AM
Last Post: heiner55
  Linear search/searching for a word in a file/list kietrichards 3 3,472 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  How to iterate through a list and search for a value fad3r 2 2,862 Jan-23-2018, 02:07 AM
Last Post: fad3r

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020