Re: A problem with itertools.groupby

2021-12-17 Thread Peter Pearson
On Fri, 17 Dec 2021 09:25:03 +0100, ast wrote: [snip] > > but: > > li = [grp for k, grp in groupby("aahfffddnnb")] > list(li[0]) > > [] > > list(li[1]) > > [] > > It seems empty ... I don't understand why, this is > the first read of an iterator, it should provide its > data. Baffling. Here'

Re: A problem with itertools.groupby

2021-12-17 Thread Chris Angelico
> list(li[1]) > > [] > > It seems empty ... I don't understand why, this is > the first read of an iterator, it should provide its > data. > https://docs.python.org/3/library/itertools.html#itertools.groupby Check the explanatory third paragraph :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: A problem with itertools.groupby

2021-12-17 Thread Antoon Pardon
but: li = [grp for k, grp in groupby("aahfffddnnb")] list(li[0]) [] list(li[1]) [] It seems empty ... I don't understand why, this is the first read of an iterator, it should provide its data. The group-iterators are connected. Each group-iterator is a wrapper around the original it

A problem with itertools.groupby

2021-12-17 Thread ast
Python 3.9.9 Hello I have some troubles with groupby from itertools from itertools import groupby for k, grp in groupby("aahfffddnnb"): print(k, list(grp)) print(k, list(grp)) a ['a', 'a'] a [] h ['h'] h [] f ['f', 'f', 'f'] f [] d ['d', 'd'] d [] s ['s', 's', 's', 's'] s [] n ['n

Re: itertools.groupby

2013-04-22 Thread Chris Angelico
On Tue, Apr 23, 2013 at 12:49 AM, Oscar Benjamin wrote: > Iterators are > typically preferred over list slicing for sequential text file access > since you can avoid loading the whole file at once. This means that > you can process a large file while only using a constant amount of > memory. And,

Re: itertools.groupby

2013-04-22 Thread Neil Cerutti
On 2013-04-22, Oscar Benjamin wrote: > On 22 April 2013 15:24, Neil Cerutti wrote: >> >> Hrmmm, hoomm. Nobody cares for slicing any more. >> >> def headered_groups(lst, header): >> b = lst.index(header) + 1 >> while True: >> try: >> e = lst.index(header, b) >>

Re: itertools.groupby

2013-04-22 Thread Oscar Benjamin
On 22 April 2013 15:24, Neil Cerutti wrote: > > Hrmmm, hoomm. Nobody cares for slicing any more. > > def headered_groups(lst, header): > b = lst.index(header) + 1 > while True: > try: > e = lst.index(header, b) > except ValueError: > yield lst[b:] >

Re: itertools.groupby

2013-04-22 Thread Neil Cerutti
On 2013-04-20, Jason Friedman wrote: > I have a file such as: > > $ cat my_data > Starting a new group > a > b > c > Starting a new group > 1 > 2 > 3 > 4 > Starting a new group > X > Y > Z > Starting a new group > > I am wanting a list of lists: > ['a', 'b', 'c'] > ['1', '2', '3', '4'] > ['X', 'Y'

Re: itertools.groupby

2013-04-22 Thread Wolfgang Maier
Jason Friedman gmail.com> writes: > > Thank you for the responses!  Not sure yet which one I will pick. > Hi again, I was working a bit on my own solution and on the one from Steven/Joshua, and maybe that helps you deciding: def separate_on(iterable, separator): # based on groupby sep

Re: itertools.groupby

2013-04-21 Thread Joshua Landau
On 21 April 2013 01:13, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > I wouldn't use groupby. It's a hammer, not every grouping job is a nail. > > Instead, use a simple accumulator: > > > def group(lines): > accum = [] > for line in lines: > line = line.strip() >

Re: itertools.groupby

2013-04-21 Thread Jason Friedman
#!/usr/bin/python3 > from itertools import groupby > > def get_lines_from_file(file_name): > with open(file_name) as reader: > for line in reader.readlines(): > yield(line.strip()) > > counter = 0 > def key_func(x): > if x.startswith("Starting a new group"): > g

Re: itertools.groupby

2013-04-21 Thread Peter Otten
list(group)[1:]) > > > I get the output I desire, but I'm wondering if there is a solution > without the global counter. If you were to drop the empty groups you could simplify it to def is_header(line): return line.startswith("Starting

Re: itertools.groupby

2013-04-20 Thread Steven D'Aprano
On Sat, 20 Apr 2013 11:09:42 -0600, Jason Friedman wrote: > I have a file such as: > > $ cat my_data > Starting a new group > a > b > c > Starting a new group > 1 > 2 > 3 > 4 > Starting a new group > X > Y > Z > Starting a new group > > I am wanting a list of lists: > ['a', 'b', 'c'] > ['1', '2'

Re: itertools.groupby

2013-04-20 Thread Wolfgang Maier
Jason Friedman gmail.com> writes: > > I have a file such as: > > $ cat my_data  > Starting a new group > > a > b > c > Starting a new group > 1 > 2 > 3 > > 4 > Starting a new group > X > Y > Z > Starting a new group > > > I am wanting a list of lists: > ['a', 'b', 'c'] > > ['1', '2', '3',

Re: itertools.groupby

2013-04-20 Thread Ned Batchelder
On 4/20/2013 1:09 PM, Jason Friedman wrote: I have a file such as: $ cat my_data Starting a new group a b c Starting a new group 1 2 3 4 Starting a new group X Y Z Starting a new group I am wanting a list of lists: ['a', 'b', 'c'] ['1', '2', '3', '4'] ['X', 'Y', 'Z'] [] I wrote this: -

itertools.groupby

2013-04-20 Thread Jason Friedman
I have a file such as: $ cat my_data Starting a new group a b c Starting a new group 1 2 3 4 Starting a new group X Y Z Starting a new group I am wanting a list of lists: ['a', 'b', 'c'] ['1', '2', '3', '4'] ['X', 'Y', 'Z'] [] I wrote this: #!/usr/bin/python3

Re: itertools.groupby usage to get structured data

2011-02-07 Thread nn
;s_v1' : 6.0, > >            's_v2' : 8.0, > >            'g2' : { > >                     8 : { > >                           's_v1' : 6.0, > >                           's_v2' : 8.0} > >            } > >        

Re: itertools.groupby usage to get structured data

2011-02-05 Thread Peter Otten
} >}, > 2 : { >'s_v1' : 6.0, >'s_v2' : 8.0, >'g2' : { > 8 : { > 's_v1' : 6.0, > 's_v2' : 8.0} >

Re: itertools.groupby usage to get structured data

2011-02-05 Thread Slafs
solution that would let me do that kind of > > grouping with variable lists of 2) and 3) i.e. having also 'g3' as > > grouping element so the 'g2' dicts could also have their own > > "subgroup" and be even more nested then. > > I was trying something

Re: itertools.groupby usage to get structured data

2011-02-04 Thread Paul Rubin
able lists of 2) and 3) i.e. having also 'g3' as > grouping element so the 'g2' dicts could also have their own > "subgroup" and be even more nested then. > I was trying something with itertools.groupby and updating nested > dicts, but as i was writing the c

Re: itertools.groupby usage to get structured data

2011-02-04 Thread Steven D'Aprano
s_v1 s_v2 g2 = = = 7.0 6.5 | s_v1 s_v2 --- 8 | 5.0 3.5 9 | 2.0 3.0 Does this help you to either (1) redesign your data structures, or (2) work out how to go from there? [...] > I was looking for a solution that would

itertools.groupby usage to get structured data

2011-02-04 Thread Slafs
's_v2' : 8.0} } }, ... } # notice the summed values of s_v1 and s_v2 when g1 == 1 I was looking for a solution that would let me do that kind of grouping with variable lists of 2) and 3) i.e. having also 'g3' as grouping element so the 'g2' dicts could also have their own "subgroup" and be even more nested then. I was trying something with itertools.groupby and updating nested dicts, but as i was writing the code it started to feel too verbose to me :/ Do You have any hints maybe? because i'm kind of stucked :/ Regards Sławek -- http://mail.python.org/mailman/listinfo/python-list

Re: bug with itertools.groupby?

2009-10-07 Thread Paul McGuire
On Oct 6, 6:06 pm, Kitlbast wrote: > > grouped acc:  61 > grouped acc:  64 > grouped acc:  61 > > am I doing something wrong? sort first, then groupby. -- http://mail.python.org/mailman/listinfo/python-list

Re: bug with itertools.groupby?

2009-10-06 Thread Dave Angel
Kitlbast wrote: On Oct 7, 3:04 am, Raymond Hettinger wrote: On Oct 6, 4:06 pm, Kitlbast wrote: Hi there, the code below on Python 2.5.2: from itertools import groupby info_list = {'profile': 'http://somesite.com/profile1', 'account': 61L}, {'profile'

Re: bug with itertools.groupby?

2009-10-06 Thread Kitlbast
On Oct 7, 3:04 am, Raymond Hettinger wrote: > On Oct 6, 4:06 pm, Kitlbast wrote: > > > > > > > Hi there, > > > the code below on Python 2.5.2: > > > from itertools import groupby > > > info_list = [ > >     {'profile': 'http://somesite.com/profile1', 'account': 61L}, > >     {'profile': 'http://s

Re: bug with itertools.groupby?

2009-10-06 Thread Raymond Hettinger
On Oct 6, 4:06 pm, Kitlbast wrote: > Hi there, > > the code below on Python 2.5.2: > > from itertools import groupby > > info_list = [ >     {'profile': 'http://somesite.com/profile1', 'account': 61L}, >     {'profile': 'http://somesite.com/profile2', 'account': 64L}, >     {'profile': 'http://som

Re: bug with itertools.groupby?

2009-10-06 Thread Kitlbast
es and it works 3 times faster then itertools.groupby for my example (for tests I extend number of profiles) -- http://mail.python.org/mailman/listinfo/python-list

Re: bug with itertools.groupby?

2009-10-06 Thread Rhodri James
On Wed, 07 Oct 2009 00:06:43 +0100, Kitlbast wrote: Hi there, the code below on Python 2.5.2: from itertools import groupby info_list = [ {'profile': 'http://somesite.com/profile1', 'account': 61L}, {'profile': 'http://somesite.com/profile2', 'account': 64L}, {'profile': 'http:

Re: bug with itertools.groupby?

2009-10-06 Thread Diez B. Roggisch
grouped acc: 64 grouped acc: 61 am I doing something wrong? http://docs.python.org/library/itertools.html#itertools.groupby """ Generally, the iterable needs to already be sorted on the same key function. """ Diez -- http://mail.python.org/mailman/listinfo/python-list

bug with itertools.groupby?

2009-10-06 Thread Kitlbast
Hi there, the code below on Python 2.5.2: from itertools import groupby info_list = [ {'profile': 'http://somesite.com/profile1', 'account': 61L}, {'profile': 'http://somesite.com/profile2', 'account': 64L}, {'profile': 'http://somesite.com/profile3', 'account': 61L}, ] grouped_by_a

Re: itertools.groupby

2008-01-16 Thread Tobiah
Paul Rubin wrote: > Tobiah <[EMAIL PROTECTED]> writes: >> I tried doing this with a simple example, but noticed >> that [].sort(func) passes two arguments to func, whereas >> the function expected by groupby() uses only one argument. > > Use: [].sort(key=func) Oh cool. Thanks. Only in 2.4+ it s

Re: itertools.groupby

2008-01-16 Thread Paul Rubin
Tobiah <[EMAIL PROTECTED]> writes: > I tried doing this with a simple example, but noticed > that [].sort(func) passes two arguments to func, whereas > the function expected by groupby() uses only one argument. Use: [].sort(key=func) -- http://mail.python.org/mailman/listinfo/python-list

itertools.groupby

2008-01-15 Thread Tobiah
In the docs and examples that I have seen so far, it is suggested that the groupby() input list may be pre-sorted with the same function that is passed to groupby as a key extractor. I tried doing this with a simple example, but noticed that [].sort(func) passes two arguments to func, whereas the

Re: itertools.groupby

2007-06-05 Thread BJörn Lindqvist
e groupby method has its uses > > I'd settle for a simple explanation of what it does in python. Here is another example: import itertools import random dierolls = sorted(random.randint(1, 6) for x in range(200)) for number, numbers in itertools.groupby(dierolls): number_cou

Re: itertools.groupby

2007-06-05 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: > On May 27, 7:50 pm, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > The groupby itertool came-out in Py2.4 and has had remarkable > > success (people seem to get what it does and like using it, and > > there have been no bug reports or reports of usability problems). > >

Re: itertools.groupby

2007-06-05 Thread [EMAIL PROTECTED]
On May 27, 7:50 pm, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > The groupby itertool came-out in Py2.4 and has had remarkable > success (people seem to get what it does and like using it, and > there have been no bug reports or reports of usability problems). With due respect, I disagree. Bug

Re: itertools.groupby (gauntlet thrown down)

2007-05-29 Thread Steve Howell
> Raymond Hettinger <[EMAIL PROTECTED]> writes: > > The gauntlet has been thrown down. Any creative > thinkers > > up to the challenge? Give me cool recipes. > Twin primes? (Sorry, no code, but there's a good Python example somewhere that returns an iterator that keeps doing the sieve, feed it

Re: itertools.groupby

2007-05-29 Thread Steve Howell
s is the first paragraph. This is the second. '''.splitlines() # Use itertools.groupby and bool to return groups of # consecutive lines that either have content or don't. for has_chars, frags in itertools.groupby(lines, bool): if has_chars: print ' '.join(

Re: itertools.groupby

2007-05-29 Thread Paul Rubin
Raymond Hettinger <[EMAIL PROTECTED]> writes: > The gauntlet has been thrown down. Any creative thinkers > up to the challenge? Give me cool recipes. Here is my version (with different semantics) of the grouper recipe in the existing recipe section: snd = operator.itemgetter(1) # I use thi

Re: itertools.groupby

2007-05-29 Thread George Sakkis
On May 29, 2:34 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > If the posters on this thread have developed an interest > in the subject, I would find it useful to hear their > ideas on new and creative ways to use groupby(). The > analogy to UNIX's uniq filter was found only after the > desi

Re: itertools.groupby

2007-05-29 Thread Steve Howell
ealing with a use case where you don't need the groups themselves to be sorted. Instead, you'd use something more straightforward (and faster, I think) like the cookbook "SQL-like Group By" example. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259173 It seems t

Re: itertools.groupby

2007-05-29 Thread Carsten Haese
On Mon, 2007-05-28 at 23:34 -0700, Raymond Hettinger wrote: > On May 28, 8:36 pm, "Carsten Haese" <[EMAIL PROTECTED]> wrote: > > And while > > we're at it, it probably should be keyfunc(value), not key(value). > > No dice. The itertools.groupby() functi

Re: itertools.groupby

2007-05-29 Thread Raymond Hettinger
On May 28, 8:02 pm, Gordon Airporte <[EMAIL PROTECTED]> wrote: > "Each" seems to imply uniqueness here. Doh! This sort of micro-massaging the docs misses the big picture. If "each" meant unique across the entire input stream, then how the heck could the function work without reading in the entire

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
On May 28, 8:36 pm, "Carsten Haese" <[EMAIL PROTECTED]> wrote: > And while > we're at it, it probably should be keyfunc(value), not key(value). No dice. The itertools.groupby() function is typically used in conjunction with sorted(). It would be a mistake to call it k

Re: itertools.groupby

2007-05-28 Thread Carsten Haese
On Mon, 28 May 2007 23:02:31 -0400, Gordon Airporte wrote > ''' > class groupby(__builtin__.object) > | groupby(iterable[, keyfunc]) -> create an iterator which returns > | (key, sub-iterator) grouped by each value of key(value). > | > ''' > > "Each" seems to imply uniqueness here. Yes, I

Re: itertools.groupby

2007-05-28 Thread Paul Rubin
Gordon Airporte <[EMAIL PROTECTED]> writes: > "itertools.groupby_except_the_notion_of_uniqueness_is_limited_to- > _contiguous_runs_of_elements_having_the_same_key()" doesn't have much > of a ring to it. I guess this gets back to documentation problems, > because the help string says nothing about t

Re: itertools.groupby

2007-05-28 Thread Gordon Airporte
Paul Rubin wrote: > It chops up the iterable into a bunch of smaller ones, but the total > size ends up the same. "Telescope", "compact", "collapse" etc. make > it sound like the output is going to end up smaller than the input. Good point... I guess I was thinking in terms of the number of iter

Re: itertools.groupby

2007-05-28 Thread Paul Rubin
Paul Rubin writes: > >See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259173 > But that recipe generates the groups in a random order depending on > the dict hashing, Correction, it generates the right order in this case, although it builds up an in-memo

Re: itertools.groupby

2007-05-28 Thread Paul Rubin
t hashing, instead of keeping them in the original sequence's order, which the OP's application might well require. itertools.groupby really is the right thing. I agree that itertools is not the easiest module in the world for beginning programmers to understand, but every serious Python

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Alex Martelli <[EMAIL PROTECTED]> wrote: > Steve Howell <[EMAIL PROTECTED]> wrote: >... > > for has_chars, frags in itertools.groupby(lines, > > lambda x: len(x) > 0): > > Hmmm, it appears to me that itertools.groupby(lines, > bool) should do

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > > That's not for everyone, so it isn't a loss if > > > someone sticks > > > with writing plain, clear everyday Python > instead of > > > an itertool. > > > > I know most of the module is fairly advanced, and > that > > average users can mostly

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Paul Rubin <"http://phr.cx"@NOSPAM.invalid> wrote: > > > But that is what groupby does, except its notion of > uniqueness is > limited to contiguous runs of elements having the > same key. It occurred to me that we could also rename the function uniq(), or unique(), after its Unix counterpa

Re: itertools.groupby

2007-05-28 Thread Paul Rubin
Gordon Airporte <[EMAIL PROTECTED]> writes: > This is my first exposure to this function, and I see that it does > have some uses in my code. I agree that it is confusing, however. > IMO the confusion could be lessened if the function with the current > behavior were renamed 'telescope' or 'compact

Re: itertools.groupby

2007-05-28 Thread Gordon Airporte
7stud wrote: > Bejeezus. The description of groupby in the docs is a poster child > for why the docs need user comments. Can someone explain to me in > what sense the name 'uniquekeys' is used this example: > This is my first exposure to this function, and I see that it does have some uses in

Re: itertools.groupby

2007-05-28 Thread Alex Martelli
Steve Howell <[EMAIL PROTECTED]> wrote: ... > for has_chars, frags in itertools.groupby(lines, > lambda x: len(x) > 0): Hmmm, it appears to me that itertools.groupby(lines, bool) should do just the same job, just a bit faster and simpler, no? Alex -- http://mail.python.org/m

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
> > That's not for everyone, so it isn't a loss if > > someone sticks > > with writing plain, clear everyday Python instead of > > an itertool. > > I know most of the module is fairly advanced, and that > average users can mostly avoid it, but this is a very > common-antipattern that groupby() solv

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Raymond Hettinger <[EMAIL PROTECTED]> wrote: > That's not for everyone, so it isn't a loss if > someone sticks > with writing plain, clear everyday Python instead of > an itertool. > I know most of the module is fairly advanced, and that average users can mostly avoid it, but this is a very

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Carsten Haese <[EMAIL PROTECTED]> wrote: > On Sun, 2007-05-27 at 18:12 -0700, Steve Howell > wrote: > > [...] there is no way > > that "uniquekeys" is a sensible variable [...] > > That's because the OP didn't heed the advice from > the docs that > "Generally, the iterable needs to already b

Re: itertools.groupby

2007-05-28 Thread Raymond Hettinger
On May 28, 8:34 am, 7stud <[EMAIL PROTECTED]> wrote: > >- there are two more examples on the next page. those two > > examples also give sample inputs and outputs. > > I didn't see those. Ah, there's the rub. The two sections of examples and recipes are there for a reason. This isn't a beginne

Re: itertools.groupby

2007-05-28 Thread 7stud
s the element unchanged). An example: -- import itertools lst = [1, 2, 2, 2, 1, 1, 3] def func(num): if num == 2: return "a" else: return "b" keys = [] groups = [] for k, g in itertools.groupby(lst, func): keys.append(k) groups.append( list(g) ) pr

Re: itertools.groupby

2007-05-28 Thread Steve Howell
ge_numbers)) > for d,g in groupby(enumerate(pages), lambda > (i,p): i-p): > h = map(snd, g) > if len(h) > 1: >yield '%d-%d'% (h[0], h[-1]) > else: >yield '%d'% h[0] >print ', '.join(page

Re: itertools.groupby

2007-05-28 Thread Steve Howell
--- Raymond Hettinger <[EMAIL PROTECTED]> wrote: > + The operation of \function{groupby()} is similar > to the \code{uniq} > filter > + in \UNIX{}. [...] Thanks! The comparison of groupby() to "uniq" really clicks with me. To the extent that others like the Unix command line analogy for u

Re: itertools.groupby

2007-05-28 Thread Carsten Haese
On Sun, 2007-05-27 at 20:28 -0700, Paul Rubin wrote: >fst = operator.itemgetter(0) >snd = operator.itemgetter(1) > >def bates(fd): > # generate tuples (n,d) of lines from file fd, > # where n is the record number. Just iterate through all lines > # of the file, stamping

Re: itertools.groupby

2007-05-28 Thread Carsten Haese
On Sun, 2007-05-27 at 18:12 -0700, Steve Howell wrote: > [...] there is no way > that "uniquekeys" is a sensible variable [...] That's because the OP didn't heed the advice from the docs that "Generally, the iterable needs to already be sorted on the same key function." > http://informixdb.blogsp

Re: itertools.groupby

2007-05-27 Thread Paul Rubin
Raymond Hettinger <[EMAIL PROTECTED]> writes: > On May 27, 8:28 pm, Paul Rubin wrote: > > I use the module all the time now and it is great. > Thanks for the accolades and the great example. Thank YOU for the great module ;). Feel free to use the example in the docs i

Re: itertools.groupby

2007-05-27 Thread Raymond Hettinger
On May 27, 8:28 pm, Paul Rubin wrote: > I use the module all the time now and it is great. Thanks for the accolades and the great example. FWIW, I checked in a minor update to the docs: +++ python/trunk/Doc/lib/libitertools.tex Mon May 28 07:23:22 2007 @@ -138,6 +13

Re: itertools.groupby

2007-05-27 Thread Paul Rubin
Raymond Hettinger <[EMAIL PROTECTED]> writes: > The groupby itertool came-out in Py2.4 and has had remarkable > success (people seem to get what it does and like using it, and > there have been no bug reports or reports of usability problems). > All in all, that ain't bad (for what 7stud calls a po

Re: itertools.groupby

2007-05-27 Thread Steve Howell
--- Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > FWIW, I wrote those docs. Suggested improvements > are > welcome; however, I think they already meet a > somewhat > high standard of quality: > I respectfully disagree, and I have suggested improvements in this thread. Without even reading

Re: itertools.groupby

2007-05-27 Thread Steve Howell
sertion of uniqueness implicit in the name of the variable doesn't make any sense. > There is an > example on the following page, called Examples! > The example is useful. Thank you. > > These docs need work. Please do not defend them; > > [...] > To name just

Re: itertools.groupby

2007-05-27 Thread Raymond Hettinger
On May 27, 2:59 pm, Steve Howell <[EMAIL PROTECTED]> wrote: > These docs need work. Please do not defend them; > please suggest improvements. FWIW, I wrote those docs. Suggested improvements are welcome; however, I think they already meet a somewhat high standard of quality: - there is an accur

Re: itertools.groupby

2007-05-27 Thread Carsten Haese
On Sun, 2007-05-27 at 14:59 -0700, Steve Howell wrote: > Huh? How is code that uses itertools.groupby not an > actual example of using itertools.groupby? Here's how: """ The returned group is itself an iterator that shares the underlying iterable with groupby(). Be

Re: itertools.groupby

2007-05-27 Thread Steve Howell
he name 'uniquekeys' is used this > example: > > > > > > import itertools > > > > mylist = ['a', 1, 'b', 2, 3, 'c'] > > > > def isString(x): > > s = str(x) > > if s == x: > > return

Re: itertools.groupby

2007-05-27 Thread Steve Howell
--- paul <[EMAIL PROTECTED]> wrote: > > > > Regarding the pitfalls of groupby in general (even > > assuming we had better documentation), I invite > people > > to view the following posting that I made on > > python-ideas, entitled "SQL-like way to manipulate > > Python data structures": > > >

Re: itertools.groupby

2007-05-27 Thread paul
Steve Howell schrieb: > --- Steve Howell <[EMAIL PROTECTED]> wrote: > >> --- 7stud <[EMAIL PROTECTED]> wrote: >> >>> Bejeezus. The description of groupby in the docs >> is >>> a poster child >>> for why the docs need user comments. > > Regarding the pitfalls of groupby in general (even > assum

Re: itertools.groupby

2007-05-27 Thread Carsten Haese
gt; > mylist = ['a', 1, 'b', 2, 3, 'c'] > > def isString(x): > s = str(x) > if s == x: > return True > else: > return False > > uniquekeys = [] > groups = [] > for k, g in itertools.groupby(mylist,

Re: itertools.groupby

2007-05-27 Thread Steve Howell
--- Steve Howell <[EMAIL PROTECTED]> wrote: > > --- 7stud <[EMAIL PROTECTED]> wrote: > > > Bejeezus. The description of groupby in the docs > is > > a poster child > > for why the docs need user comments. > Regarding the pitfalls of groupby in general (even assuming we had better documenta

Re: itertools.groupby

2007-05-27 Thread Steve Howell
--- 7stud <[EMAIL PROTECTED]> wrote: > > I'd settle for a simple explanation of what it does > in python. > The groupby function prevents you have from having to write awkward (and possibly broken) code like this: group = [] lastKey = None for item in items: newKey = item.k

Re: itertools.groupby

2007-05-27 Thread Steve Howell
.. import itertools syslog_messages = [ 'out of file descriptors', 'out of file descriptors', 'unexpected access', 'out of file descriptors', ] for message, messages in itertools.groupby(syslog_messages): print message, len(list(messages)) ...p

Re: itertools.groupby

2007-05-27 Thread 7stud
On May 27, 11:28 am, Steve Howell <[EMAIL PROTECTED]> wrote: > --- 7stud <[EMAIL PROTECTED]> wrote: > > Bejeezus. The description of groupby in the docs is > > a poster child > > for why the docs need user comments. Can someone > > explain to me in > > what sense the name 'uniquekeys' is used thi

Re: itertools.groupby

2007-05-27 Thread Steve Howell
--- 7stud <[EMAIL PROTECTED]> wrote: > Bejeezus. The description of groupby in the docs is > a poster child > for why the docs need user comments. Can someone > explain to me in > what sense the name 'uniquekeys' is used this > example: [...] > The groupby method has its uses, but it's behavi

itertools.groupby

2007-05-27 Thread 7stud
sString(x): s = str(x) if s == x: return True else: return False uniquekeys = [] groups = [] for k, g in itertools.groupby(mylist, isString): uniquekeys.append(k) groups.append(list(g)) print uniquekeys print groups --output:-- [True, False, True, False, True] [[&#

Re: Problem with itertools.groupby.

2006-05-25 Thread Fredrik Lundh
> itertools only looks for changes to the key value (the one returned by > operator.itemgetter(0) in your case); it doesn't sort the list for you. > > this should work: > >for k, g in itertools.groupby(sorted(vals), operator.itemgetter(0)): > print k, [i for

Re: Problem with itertools.groupby.

2006-05-25 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > What am I doing wrong here? > >>>> import operator >>>> import itertools >>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15), > ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)] >>>> for k,

Re: Problem with itertools.groupby.

2006-05-25 Thread Scott David Daniels
[EMAIL PROTECTED] wrote: > What am I doing wrong here? > >>>> import operator >>>> import itertools >>>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15), > ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)] >>>> for k,

Problem with itertools.groupby.

2006-05-25 Thread trebucket
What am I doing wrong here? >>> import operator >>> import itertools >>> vals = [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15), ... (1, 16), (2, 17), (3, 18), (4, 19), (5, 20)] >>> for k, g in itertools.groupby(iter(vals), operator.itemgetter(0)): ... p