0

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?

misterkandle
  • 135
  • 2
  • 14
  • 3
    Welcome, It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – MooingRawr Jan 25 '18 at 14:22
  • Perhaps try with a simple for loop where you append to a new 'merged-list'. In the loop you look ahead and compare the key of the next index, and do the appropriate merging if they match. Also, I assume the numbers are strings rather than plain integers? Otherwise I don't see how you would merge them like such. - Also agree with @MooingRawr, hence the limited answer. – Emil L. Jan 25 '18 at 14:23
  • @MooingRawr not really asking todo the code for me. I have tried basic lambdas like: strings.sort(key=lambda element: (e[0], e[1])) which have sorted them out properly but I don't know how to use the join to merge them once they are sorted out – misterkandle Jan 25 '18 at 14:27
  • The old itertools docs had a recipe for grouping consecutive items. It's illustrated [here](https://stackoverflow.com/a/2154437/4014959). – PM 2Ring Jan 25 '18 at 14:33
  • 2
    @mistervela the point is, show what you've tried even if it doesn't work. It shows you have put effort into solving your own question and maybe someone can fix what you were doing wrong building off of what you've done. – MooingRawr Jan 25 '18 at 14:37
  • Why is `['monkey','28', '90']` not considered consecutive? – pylang Jan 26 '18 at 06:01
  • How are this sorted `['monkey', '4-5', '20-25'] , ['monkey', '28', '90']`? – pylang Jan 26 '18 at 06:41

1 Answers1

1

you can try something like this and take hint from this code:

data=[['lion', '23', '35'],
['lion', '24', '68'],
['elephant', '23', '34'],
['monkey', '4', '20'],
['monkey', '5' , '25'],
['monkey','28', '90'],
['lion', '33', '2']]

from itertools import groupby
from itertools import combinations
final_=[]
for i,j in groupby(sorted(data),key=lambda x:x[0]):
    final_.append(list(j))

final__=[]
check=[]
for mn in final_:
    if len(mn)==1:
        final__.append(mn)
    for k in combinations(mn,r=2):

        if abs(int(k[0][1])-int(k[1][1]))==1:
            check.extend(k)
            final__.append([k[0][0],"{}-{}".format(k[0][1],k[1][1]),"{}-{}".format(k[0][2],k[1][2])])
        else:
            if k[0] not in check:
                if k[0] not in final__:
                    final__.append(k[0])
            elif k[1] not in check:
                if k[1] not in final__:
                    final__.append(k[1])

print(final__)

output:

[[['elephant', '23', '34']], ['lion', '23-24', '35-68'], ['lion', '33', '2'], ['monkey', '28', '90'], ['monkey', '4-5', '20-25']]