I have a list of lists like this:
['lion', '23', '35']
['lion', '24', '68']
['elephant', '23', '34']
['monkey', '4', '20']
['monkey', '5', '25']
['monkey','28', '90']
['lion', '33', '2']
and I want to sort them out and merge the ones with the same type of animal that have consecutive values (23-34) and merge as well its adjacent values (i.e. 35-68 for lion) , like this:
['lion', '23-24', '35-68']
['lion', '33', '2']
['elephant', '23', '34']
['monkey', '4-5', '20-25']
['monkey', '28', '90']
Notice that the consecutive values for lion and monkey were merged and separated with a dash as well as the sequence next to it. The ones that were not consecutive remained alone: lion 33 and monkey 28.
I have tried some code with itertools and groupby as well as labda sorting to no avail.
For instance
strings.sort(key=lambda element: (element[0], element[1]))
Sorts them by animal name and consecutive numbers but I would not know how to merge them.
What I want is order them alphabetically by the name of the animal and merge the ones that have a consecutive second value and create a new list with that output.
Any ideas?