How can I find same values in a list and group together a new list?

From this list:

N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]

I’m trying to create:

L = [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]

Any value which is found to be the same is grouped into it’s own sublist.
Here is my attempt so far, I’m thinking I should use a while loop?

global n

n = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] #Sorted list
l = [] #Empty list to append values to

def compare(val):
   """ This function receives index values
   from the n list (n[0] etc) """
   
   global valin
   valin = val

   global count
   count = 0

    for i in xrange(len(n)):
        if valin == n[count]: # If the input value i.e. n[x] == n[iteration]
            temp = valin, n[count]
             l.append(temp) #append the values to a new list
             count +=1
        else:
          count +=1
    

for x in xrange (len(n)):
    compare(n[x]) #pass the n[x] to compare function

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use itertools.groupby:

from itertools import groupby

N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]

print([list(j) for i, j in groupby(N)])

Output:

[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]

Side note: Prevent from using global variable when you don’t need to.

Method 2

Someone mentions for N=[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 1] it will get [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5], [1]]

In other words, when numbers of the list isn’t in order or it is a mess list, it’s not available.

So I have better answer to solve this problem.

from collections import Counter

N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
C = Counter(N)

print [ [k,]*v for k,v in C.items()]

Method 3

You can use itertools.groupby along with a list comprehension

>>> l =  [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
>>> [list(v) for k,v in itertools.groupby(l)]
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]

This can be assigned to the variable L as in

L = [list(v) for k,v in itertools.groupby(l)]

Method 4

You’re overcomplicating this.

What you want to do is: for each value, if it’s the same as the last value, just append it to the list of last values; otherwise, create a new list. You can translate that English directly to Python:

new_list = []
for value in old_list:
    if new_list and new_list[-1][0] == value:
        new_list[-1].append(value)
    else:
        new_list.append([value])

There are even simpler ways to do this if you’re willing to get a bit more abstract, e.g., by using the grouping functions in itertools. But this should be easy to understand.


If you really need to do this with a while loop, you can translate any for loop into a while loop like this:

for value in iterable:
    do_stuff(value)

iterator = iter(iterable)
while True:
    try:
        value = next(iterator)
    except StopIteration:
        break
    do_stuff(value)

Or, if you know the iterable is a sequence, you can use a slightly simpler while loop:

index = 0
while index < len(sequence):
    value = sequence[index]
    do_stuff(value)
    index += 1

But both of these make your code less readable, less Pythonic, more complicated, less efficient, easier to get wrong, etc.

Method 5

You can do that using numpy too:

import numpy as np

N = np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
counter = np.arange(1, np.alen(N))
L = np.split(N, counter[N[1:]!=N[:-1]])

The advantage of this method is when you have another list which is related to N and you want to split it in the same way.

Method 6

Another slightly different solution that doesn’t rely on itertools:

#!/usr/bin/env python

def group(items):
    """
    groups a sorted list of integers into sublists based on the integer key
    """
    if len(items) == 0:
        return []

    grouped_items = []
    prev_item, rest_items = items[0], items[1:]

    subgroup = [prev_item]
    for item in rest_items:
        if item != prev_item:
            grouped_items.append(subgroup)
            subgroup = []
        subgroup.append(item)
        prev_item = item

    grouped_items.append(subgroup)
    return grouped_items

print group([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
# [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x