0

I am trying to implement genetic algos to optimize pathways. For that, I need to sort lists.

The basic list (list_cities) looks like this:

[[{'city': 'Pau', 'lan': 43.293295, 'lng': -0.36357}, {'city': 'Marseille', 'lan': 43.293551, 'lng': 5.377397}, {'distance': 5572.500801706894}], [{'city': 'Nice', 'lan': 43.70168, 'lng': 7.260711}, {'city': 'Lyon', 'lan': 45.759132, 'lng': 4.834604}, {'distance': 6306.2650380290725}]]

As you can see, I have a global list, containing several lists (200) containing cities themselves and a distance (representing the total distance to link all the cities in the order of the list. I would like to sort my 200 city lists, by the last value which is the distance. I tried in many ways but without success.

My last try :

sort_list = sorted(list_cities, key=lambda k: k['distance'])

Which give me the following result :

TypeError: list indices must be integers or slices, not str

6
  • It looks like your have list of lists not list of dicts, try making a list of dicts and do what you already did. Commented May 7, 2019 at 22:42
  • Is there a way to sort in the way that I want anyway? Commented May 7, 2019 at 22:43
  • Uhm, maybe with some extra steps, I'll write a solution for you. Commented May 7, 2019 at 22:44
  • 1
    If the distance object is always the last one in your lists, you can try k[-1]["distance"] Commented May 7, 2019 at 22:44
  • Indeed it's always the last one in my list @tevemadar and your solution seems to work :) Anyway I'm wondering, is this a good way to work like that ?! Did I need to think about change the structure of this list for an other during the first build of the list. The original cities come from a json file, but maybe I badly build my list the first time ?! Commented May 7, 2019 at 22:49

1 Answer 1

1

The [[{},{}],[{},{}]] thing is a list of lists containing objects dicts (which I keep calling objects for the rest of this answer because the data is coming from JSON anyway). So in order to sort the lists by a number contained by an inner object, you have to find the given object in the list, and then get the number from it.
As the example suggests and your comment confirms, the distance is always in the last element which you can access via index -1, and thus

sort_list = sorted(list_cities, key=lambda k: k[-1]['distance'])

could work.

About the other question: I would feel it more natural to put only the cities in a list in an object:

[
 {
  'cities':[
   {'city':'Pau','lat':...,'lon':...},
   {<2nd city>},
   ...
   {<last city>}
  ],
  'distance':...
 },
 ...
]

then it would work with the original attempt.
(Sorry about the clumsy example, I am typing it on touchscreen, uh)

Sign up to request clarification or add additional context in comments.

4 Comments

Np for time, you helped me a lot ! I'll think about change the structure with your advices. Thank you very much !
" is a list of lists containing objects." everything is an object in Python. A list is an object, an int is an object, classes, functions, they are all objects. Coming from Javascript, you might be tempted to call {} an "object", and you would be technically correct, but totally ambiguous. That is a dict. So the OP is working with a list object of list objects containing dict objects.
Thanks for etymology tips, I'll change the title for more precision
@juanpa.arrivillaga thank you, added some words about it at the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.