1

I have a list of tuples [(city, country)] and I have a large XML file which contains more information about the city.

Right now I take a tuple at a time and parse the XML file. Then I get a list of all the cities and compare their names with the city of my tuple. As I do that again and again for every tuple it is really slow so I would like to save the cities of the XML somehow (connected to their extra information). What would be an efficient way concerning that I want to search through it by the name of the city in the tuple?

At first I thought about objects in a set ... But is it possible to search quickly for an object with e.g. the attribute city = "Berlin"?

Thanks for any suggestions!

steph
  • 555
  • 2
  • 6
  • 21
  • Have you considered a simple dictionary? – Frédéric Hamidi May 12 '15 at 14:15
  • Not yet, how would you do that? – steph May 12 '15 at 14:16
  • 1
    Well, you would accumulate the data in a [dict](https://docs.python.org/3.4/library/stdtypes.html#dict) instead of a set, using the city names as the keys. – Frédéric Hamidi May 12 '15 at 14:20
  • 1
    @StefanieBeutke: instead of parsing the same XML file again and again on each iteration, start with parsing the xml file, storing cities names as keys (or even better `(city, country)` tuples if possible) and cities infos as values. Then you can iterate on your `[(city, country)] ` list and lookup the dict directly. – bruno desthuilliers May 12 '15 at 14:22
  • 1
    while the above comment are my suggestion, I suggest to 'dump' the dictionary at then end and 'load' it back in the future. So you don't need to parse the XML everytime when you run the program. This is ofcourse make sense if the XML file is not changing. take a look at [this](http://stackoverflow.com/questions/11218477/how-to-use-pickle-to-save-a-dict). post – Moj May 12 '15 at 16:02

0 Answers0