csstudent
csstudent

Reputation: 107

Grouping lists of numbers together in Python

I have the following which is a list of a list in Python, and is a partial list of values that I have:

[1,33]
[2,10,42]
[5,1,33,44]
[10,42,98]
[44,12,100,124]

Is there a way of grouping them so they collect the values that are common in each list?

For example, if I look at the first list [1,33], I can see that the value exists in the third list: [5,1,33,44]

So, those are grouped together as

[5,1,33,44]

If I carry on looking, I can see that 44 is in the final list, and so that will be grouped along with this list.

[44,12,100,124] is added onto [5,1,33,44]

to give: [1,5,12,33,44,100,124]

The second list [2,10,42] has common values with [10,42,98] and are therefore joined together to give: [2,10,42,98]

So the final lists are:

[1,5,12,33,44,100,124]

[2,10,42,98]

I am guessing there is a specific name for this type of grouping. Is there a library available that can deal with it automatically? Or would I have to write a manual way of searching?

I hope the edit makes it clearer as to what I am trying to achieve.

Thanks.

Upvotes: 0

Views: 200

Answers (1)

Brad Solomon
Brad Solomon

Reputation: 40918

Here's a solution that does not require anything from the standard library or 3rd party packages. Note that this will modify a. To avoid that, just make a copy of a and work with that. The result is a list of lists containing your resulting sorted lists.

a = [
     [1,33],
     [2,10,42],
     [5,1,33,44],
     [10,42,98],
     [44,12,100,124]
    ]

res = []
while a:
    el = a.pop(0)
    res.append(el)
    for sublist in a:
        if set(el).intersection(set(sublist)):
            res[-1].extend(sublist)
            a.remove(sublist)
res = [sorted(set(i)) for i in res]
print(res)
# [[1, 5, 12, 33, 44, 100, 124], [2, 10, 42, 98]]

How this works:

  • Form an empty result list res. Groupings from a will be "transferred" here.
  • .pop() off the first element of a. This modifies a in place and defines el as that element.
  • Then loop through each sublist in a, comparing your popped el to those sublists and "building up" common sets. This is where your problem is a tiny bit tricky in that you need to gradually increment your intersected set rather than finding the intersection of multiple sublists all at once.
  • Repeat this process until a is empty.

Alternatively, if you just want to group together the even- and odd-numbered sublists (still a bit unclear from your question), you can use itertools:

from itertools import chain
grp1 = sorted(set(chain.from_iterable(a[::2])))
grp2 = sorted(set(chain.from_iterable(a[1::2])))
print(grp1)
print(grp2)
# [1, 5, 12, 33, 44, 100, 124]
# [2, 10, 42, 98]

Upvotes: 1

Related Questions