Solved

How do I count sequences in Python with itertools groupby?

Posted on 2016-11-16
3
119 Views
Last Modified: 2016-11-17
I'm trying to use the itertools groupby function here:
https://docs.python.org/2/library/itertools.html#itertools.groupby

but there are no examples of how to do this with count.  Doing the following:
import itertools as itr
In [204]: list(itr.groupby('1222311'))
Out[204]:
[('1', <itertools._grouper at 0x3cae048>),
 ('2', <itertools._grouper at 0x3cae630>),
 ('3', <itertools._grouper at 0x3caedd8>),
 ('1', <itertools._grouper at 0x3cae1d0>)]

Open in new window

yields some indecipherable second element of each tuple.  What I want to get back from that is:
In [204]: list(itr.groupby('1222311'))
Out[204]:
[('1', 1),
 ('2', 3),
 ('3', 1),
 ('1', 2)]

Open in new window

though the order of elements within the tuples is irrelevant.  
I tried adding a second argument to the groupby (e.g. key=itr.count), but they all give errors.  How do I accomplish this and how do I use the keyfunc parameter of groupby?
0
Comment
Question by:ugeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 500 total points
ID: 41891826
The undecipherable object grouper is a lazy iterator, so in order to get the count for each of those elements, you need to get their len.

import itertools as itr
for a, b  in itr.groupby('1222311'):
    print("(%s,%s)" % (a, sum(1 for i in b)))

Open in new window


you could also write like this:
import itertools as itr
for a, b  in itr.groupby('1222311'):
    print("(%s,%s)" % (a, len(list(b))))

Open in new window

0
 
LVL 11

Author Comment

by:ugeb
ID: 41892272
Thank you, that's helpful.  What exactly is the iter group iterating? We already have the token, and then you're iterating over something that gives you a count.  It looks like, after some testing, that it's just the elements of the group.  I guess that's more useful in different scenarios with a more complicated grouping function.

How do I use the keyfunc parameter?  What functions would work there?  I've tried many different things, and they all give errors.
0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 41892380
Okay, thanks to your help I figured out how to do it as a single list.  This is what I wanted to do:

[ (i[0],len(list(i[1]))) for i in itr.groupby('1113334')]
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question