Re: count consecutive elements

2021-09-27 Thread Bischoop
I'd wish to have this challenge posted on my blog but because I suck in IT english, could you guys help me with decribing the task of what the code supposed to do? -- Thanks -- https://mail.python.org/mailman/listinfo/python-list

Re: count consecutive elements

2021-01-19 Thread Peter Otten
On 19/01/2021 10:42, Bischoop wrote: On 2021-01-19, Peter Otten <__pete...@web.de> wrote: On 19/01/2021 04:45, Bischoop wrote: I sat to it again and solved it. Congratulations! lil = tuple(set(s)) # list of characters in s li=[0,0,0,0,0,0] # list for counted repeats I see a minor probl

Re: count consecutive elements

2021-01-19 Thread Bischoop
On 2021-01-19, Peter Otten <__pete...@web.de> wrote: > On 19/01/2021 04:45, Bischoop wrote: > >> I sat to it again and solved it. > > Congratulations! > > > lil = tuple(set(s)) # list of characters in s > > > > li=[0,0,0,0,0,0] # list for counted repeats > > I see a minor problem here. What happen

Re: count consecutive elements

2021-01-19 Thread Peter Otten
On 19/01/2021 04:45, Bischoop wrote: I sat to it again and solved it. Congratulations! > lil = tuple(set(s)) # list of characters in s > > li=[0,0,0,0,0,0] # list for counted repeats I see a minor problem here. What happens if s contains more than len(li) characters? import timeit Since

Re: count consecutive elements

2021-01-18 Thread Bischoop
On 2021-01-14, Stefan Ram wrote: > > If you want to know why, maybe you should insert print > statements to see the values of critical variables and > expression, especially in loops. > > Then compare the printed values with your expectations. > > Also, decompose into meaningful functio

Re: count consecutive elements

2021-01-17 Thread Peter Otten
On 17/01/2021 02:15, Dan Stromberg wrote: IMO a good set of tests is much more important than type annotations ;) def get_longest(string: str) -> typing.Tuple[int, typing.List[str]]: """Get the longest run of a single consecutive character.""" May I ask why you artificially limit th

Re: count consecutive elements

2021-01-16 Thread Dan Stromberg
On Thu, Jan 14, 2021 at 2:01 PM Wolfram Hinderer via Python-list < python-list@python.org> wrote: > Am 13.01.2021 um 22:20 schrieb Bischoop: > > I want to to display a number or an alphabet which appears mostly > > consecutive in a given string or numbers or both > > Examples > > s= ' aabskaaabad

Re: count consecutive elements

2021-01-15 Thread Tim Chase
On 2021-01-16 03:32, Bischoop wrote: >> The OP didn't specify what should happen in that case, so it would >> need some clarification. > > In that case maybe good solution would be to return three of them? That's the solution I chose in my initial reply, you get a tuple back of ([list of longest

Re: count consecutive elements

2021-01-15 Thread Bischoop
On 2021-01-14, Tim Chase wrote: > > seems to only return one value so seems to get odd results if I > specify something like > > get_longest("aaabcccbbb") > > which at least here tells me that "c" is the longest run, even though > aaa, bbb, and ccc are all runs of length 3. The OP didn't specif

Re: count consecutive elements

2021-01-14 Thread Wolfram Hinderer via Python-list
Am 13.01.2021 um 22:20 schrieb Bischoop: I want to to display a number or an alphabet which appears mostly consecutive in a given string or numbers or both Examples s= ' aabskaaabad' output: c # c appears 4 consecutive times 8bbakebaoa output: b #b appears 2 consecutive times You can

Re: count consecutive elements

2021-01-14 Thread Bischoop
On 2021-01-13, Bischoop wrote: I know what was wrong: > m = s.index(i) I forgot that m will return first index of i. So tried that way but still getting out of index although I that that I'm making sure not to get out of index. s = 'aabskaaabadh' c = 0 t = list(set(s)) # list

Re: count consecutive elements

2021-01-14 Thread Dan Stromberg
On Wed, Jan 13, 2021 at 6:20 PM Dan Stromberg wrote: > On Wed, Jan 13, 2021 at 5:59 PM Tim Chase > wrote: > >> On 2021-01-13 21:20, Bischoop wrote: >> > I want to to display a number or an alphabet which appears mostly >> > consecutive in a given string or numbers or both >> > Examples >> > s=

Re: count consecutive elements

2021-01-13 Thread Tim Chase
On 2021-01-13 18:20, Dan Stromberg wrote: > I'm kind of partial to: > > import collections > import typing > > > def get_longest(string: str) -> typing.Tuple[int, str]: > """Get the longest run of a single consecutive character.""" > dict_: typing.DefaultDict[str, int] = > collections.de

Re: count consecutive elements

2021-01-13 Thread Dan Stromberg
On Wed, Jan 13, 2021 at 5:59 PM Tim Chase wrote: > On 2021-01-13 21:20, Bischoop wrote: > > I want to to display a number or an alphabet which appears mostly > > consecutive in a given string or numbers or both > > Examples > > s= ' aabskaaabad' > > output: c > > # c appears 4 consecutive ti

Re: count consecutive elements

2021-01-13 Thread Tim Chase
On 2021-01-13 21:20, Bischoop wrote: > I want to to display a number or an alphabet which appears mostly > consecutive in a given string or numbers or both > Examples > s= ' aabskaaabad' > output: c > # c appears 4 consecutive times > 8bbakebaoa > output: b > #b appears 2 consecutive times I

Re: count consecutive elements

2021-01-13 Thread Bischoop
On 2021-01-13, Bischoop wrote: > t = set(s) # set of characters in s I have this one changed to: t= list(set(s)) -- https://mail.python.org/mailman/listinfo/python-list

Re: count points where is within the polygons using shapely and fiona

2017-01-30 Thread Xristos Xristoou
tell your proposal to count -- https://mail.python.org/mailman/listinfo/python-list

Re: count points where is within the polygons using shapely and fiona

2017-01-29 Thread Peter Pearson
On Sun, 29 Jan 2017 11:42:47 -0800 (PST), Xristos Xristoou wrote: > i tried to count points from the point shapefile where is within in > the polygon shapefile but i fail.Maybe my code is complete wrong but i > tried. > any idea how to fix my code ? > i want fast method because i have big data feat

Re: Count each unique element in list of lists

2013-11-08 Thread Vlastimil Brom
2013/11/8 Yaşar Arabacı : > Hi, > > I have a function that returns something like this; > > [[[1, 5, 9], [2, 6, 7], [3, 4, 8]], [[1, 6, 8], [2, 4, 9], [3, 5, 7]]] > > It is a list of list of lists. Each uppermost list is called a result. > I want to write a code that > shows that each elem in subli

Re: Count each unique element in list of lists

2013-11-08 Thread Chris Angelico
On Sat, Nov 9, 2013 at 6:28 AM, Yaşar Arabacı wrote: > I want to write a code that > shows that each elem in sublists of result on appears once in whole > sublist in order to add it to > my doctest. So, to clarify the problem: You want to ensure that every element occurs exactly once, neither mor

Re: Count each unique element in list of lists

2013-11-08 Thread yasar11732
This works; >>> for result in results: flat = list(item for group in result for item in group) print [sum([1 for el in flat if el==current]) for current in flat] [1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1] But I am still open to suggestions if anyone thinks t

Re: count strangeness

2011-05-22 Thread Roy Smith
In article , James Stroud wrote: > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ... def __init__(self): > ... se

Re: count strangeness

2011-05-22 Thread Peter Otten
James Stroud wrote: > Peter Otten wrote: >> James Stroud wrote: >>> WTF? >> >> Put the code into a file, run it -- and be enlightened ;) > > > tal 72% python2.7 eraseme.py > 1 > 2 > 4 > 8tal 73% cat eraseme.py > #! /usr/bin/env python > > class C: >def __init__(self): > self.data = []

Re: count strangeness

2011-05-22 Thread James Stroud
Chris Rebert wrote: On Sat, May 21, 2011 at 11:32 PM, James Stroud wrote: Chris Rebert wrote: WTF? Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. Ah. This is why it's better to be more explicit about what you

Re: count strangeness

2011-05-22 Thread James Stroud
Peter Otten wrote: James Stroud wrote: WTF? Put the code into a file, run it -- and be enlightened ;) Compare the follower to the last. tal 77% cat eraseme.py #! /usr/bin/env python class C: def __init__(self): self.data = [] def doit(self, count=[0]): for c in self.data:

Re: count strangeness

2011-05-22 Thread Chris Rebert
On Sat, May 21, 2011 at 11:32 PM, James Stroud wrote: > Chris Rebert wrote: >>> >>> WTF? >> >> Assuming your question is "Why is 1024 there twice?", the answer is > > The question is "Why is 1024 there at all?" It should be 10. Ah. This is why it's better to be more explicit about what your quest

Re: count strangeness

2011-05-21 Thread James Stroud
James Stroud wrote: Chris Rebert wrote: WTF? Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. James I mean 11, not 10--but you get the point. James -- http://mail.python.org/mailman/listinfo/python-list

Re: count strangeness

2011-05-21 Thread James Stroud
Peter Otten wrote: James Stroud wrote: WTF? Put the code into a file, run it -- and be enlightened ;) tal 72% python2.7 eraseme.py 1 2 4 8tal 73% cat eraseme.py #! /usr/bin/env python class C: def __init__(self): self.data = [] def doit(self, count=0): for c in self.data:

Re: count strangeness

2011-05-21 Thread James Stroud
Chris Rebert wrote: WTF? Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. James -- http://mail.python.org/mailman/listinfo/python-list

Re: count strangeness

2011-05-21 Thread Peter Otten
James Stroud wrote: > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ... def __init__(self): > ... self.data = [] >

Re: count strangeness

2011-05-21 Thread Chris Rebert
On Sat, May 21, 2011 at 11:02 PM, James Stroud wrote: > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ...   def __init__(

Re: count

2009-07-09 Thread J. Cliff Dyer
Bearophile wins! (This only times the loop itself. It doesn't check for __len__) summer:5 0:00:00.51 bearophile:5 0:00:00.09 summer:50 0:00:00.30 bearophile:50 0:00:00.13 summer:500 0:00:00.77 bearophile:500 0:00:00.53 summer:5000 0:00:00.000575 bearophile:5000 0:00:00.00

Re: count

2009-07-09 Thread Bearophile
Paul Rubin: > print x, sum(1 for _ in g) Don't use that, use my function :-) If g has a __len__ you are wasting time. And sum(1 ...) is (on my PC) slower. J. Clifford Dyer: > if __name__ == '__main__': >     test_func(summer, 1000) >     test_func(tupler, 1000) >     test_func(summer, 10

Re: count

2009-07-09 Thread Paul Rubin
a...@pythoncraft.com (Aahz) writes: > When dealing with small N, O() can get easily swamped by the constant > factors. How often do you deal with more than a hundred fields? The number of fields in the OP's post was not stated. Expecting it to be less than 100 seems like an ill-advised presumpti

Re: count

2009-07-08 Thread J. Clifford Dyer
On Wed, 2009-07-08 at 14:45 -0700, Paul Rubin wrote: > a...@pythoncraft.com (Aahz) writes: > > >Avoid that len(tuple(g)), use something like the following, it's lazy > > >and saves some memory. > > The question is whether it saves time, have you tested it? > > len(tuple(xrange(1))) ... hmm

Re: count

2009-07-08 Thread Aahz
In article <7xbpnuzw4u@ruckus.brouhaha.com>, Paul Rubin wrote: >a...@pythoncraft.com (Aahz) writes: >>Paul Rubin deleted an attribution: >>> >>>Avoid that len(tuple(g)), use something like the following, it's lazy >>>and saves some memory. >> >> The question is w

Re: count

2009-07-08 Thread Paul Rubin
a...@pythoncraft.com (Aahz) writes: > >Avoid that len(tuple(g)), use something like the following, it's lazy > >and saves some memory. > The question is whether it saves time, have you tested it? len(tuple(xrange(1))) ... hmm. -- http://mail.python.org/mailman/listinfo/python-list

Re: count

2009-07-08 Thread Paul Rubin
Bearophile writes: > >     print x, len(tuple(g)) > > Avoid that len(tuple(g)), use something like the following print x, sum(1 for _ in g) -- http://mail.python.org/mailman/listinfo/python-list

Re: count

2009-07-08 Thread Aahz
In article <050094ea-faf4-4e03-875d-9c2c63090...@y17g2000yqn.googlegroups.com>, Bearophile wrote: >Vilya Harvey: >> >> from itertools import groupby >> for x, g in groupby([fields[1] for fields in data]): >> =A0 =A0 print x, len(tuple(g)) > >Avoid that len(tuple(g)), use something like the follow

Re: count

2009-07-08 Thread Bearophile
Vilya Harvey: > from itertools import groupby > for x, g in groupby([fields[1] for fields in data]): >     print x, len(tuple(g)) Avoid that len(tuple(g)), use something like the following, it's lazy and saves some memory. def leniter(iterator): """leniter(iterator): return the length of a g

Re: count

2009-07-08 Thread Tim Chase
I wanted to sort column 2 in assending order and I read whole file in array "data" and did the following: data.sort(key = lambda fields:(fields[2])) I have sorted column 2, however I want to count the numbers in the column 2. i.e. I want to know, for example, how many repeates of say '3' (first

Re: count

2009-07-08 Thread Vilya Harvey
2009/7/8 Dhananjay : > I wanted to sort column 2 in assending order  and I read whole file in array > "data" and did the following: > > data.sort(key = lambda fields:(fields[2])) > > I have sorted column 2, however I want to count the numbers in the column 2. > i.e. I want to know, for example, how

Re: count secton of data in list

2009-02-23 Thread S Arrowsmith
In article <3ed253bb-d6ec-4f47-af08-ad193e9c4...@h16g2000yqj.googlegroups.com>, odeits wrote: >def count_consecutive(rows): >switch =3D 0 >count =3D 0 >for r in rows: >if r[-1] =3D=3D switch: >count +=3D 1 >else: >switch =3D not switch >

Re: count secton of data in list

2009-02-21 Thread odeits
On Feb 20, 3:45 pm, Emile van Sebille wrote: > brianrpsgt1 wrote: > > > def step1(val): > >        data2_row = [] > > >     for d1r in data1_row: > >         if d1r[1] >= val: > >             switch = 0 > >             data2_row = d1r[0],d1r[1],d1r[2],switch > >                data2_row.append([d1

Re: count secton of data in list

2009-02-20 Thread Emile van Sebille
brianrpsgt1 wrote: def step1(val): data2_row = [] for d1r in data1_row: if d1r[1] >= val: switch = 0 data2_row = d1r[0],d1r[1],d1r[2],switch data2_row.append([d1r[0],d1r[1],d1r[2],switch]) HTH, Emile -- http://mail.python.org/mailma

Re: count pages in a pdf

2007-11-27 Thread Jon Ribbens
On 2007-11-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > is it possible to parse a pdf file in python? for starters, i would > like to count the number of pages in a pdf file. i see there is a > project called ReportLab, but it seems to be a pdf generator... i > can't tell if i would be able

Re: count pages in a pdf

2007-11-27 Thread Andreas Lobinger
Tim Golden wrote: > [EMAIL PROTECTED] wrote: > >> is it possible to parse a pdf file in python? for starters, i would >> like to count the number of pages in a pdf file. i see there is a >> project called ReportLab, but it seems to be a pdf generator... i >> can't tell if i would be able to pars

Re: count pages in a pdf

2007-11-27 Thread Tim Golden
[EMAIL PROTECTED] wrote: > is it possible to parse a pdf file in python? for starters, i would > like to count the number of pages in a pdf file. i see there is a > project called ReportLab, but it seems to be a pdf generator... i > can't tell if i would be able to parse a pdf file programmically

Re: count increment...

2007-11-02 Thread Hendrik van Rooyen
Beema shafreen wrote: 8< --- file >my script: > >#!/usr/bin/env python > > >fh = open('complete_span','r') >line = fh.readline().split('#') >old_probe = line[0].strip() >old_value = line[1].strip() >print old_probe, old_value >count = 1 Better to start the c

Re: Count nb call of a function, without global var or decorator

2007-02-06 Thread Duncan Booth
"Méta-MCI" <[EMAIL PROTECTED]> wrote: > Example, with meta-data (attributs of function) : > > def ff(this): > try: > this.count=this.count+1 > except: > this.count=1 > a=1 > b=2 > c=a+b > > ff(ff) > fa=ff > ff(ff) > fa(fa) > print ff.count > > > > How to im

Re: Count nb call of a function, without global var or decorator

2007-02-05 Thread Bjoern Schliessmann
Méta-MCI wrote: > If the iterator is extern (to the function), it's like decorator, > or global var. Please excuse me, I don't understand your point. I'm not even sure if both of us speak of the same iterators. > If it is internal, it's huge, compare to this.count=this.count+1 > (or this.count

Re: Count nb call of a function, without global var or decorator

2007-02-05 Thread M�ta-MCI
Re! >>> why you didn't use an iterator? If the iterator is extern (to the function), it's like decorator, or global var. If it is internal, it's huge, compare to this.count=this.count+1 (or this.count+=1) @+ Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list

Re: Count nb call of a function, without global var or decorator

2007-02-05 Thread Bjoern Schliessmann
Méta-MCI wrote: > Example, with meta-data (attributs of function) : Apart from asking what counting "nb call" of a function means, I wonder why you didn't use an iterator? > @-salutations @-less Regards, Björn -- BOFH excuse #65: system needs to be rebooted -- http://mail.python.org/mail

Re: Count nb call of a function, without global var or decorator

2007-02-05 Thread M�ta-MCI
Re! I can do : def ff(): this=ff try: this.count=this.count+1 except: this.count=1 a=1 b=2 c=a+b ff() fa=ff ff() fa() print ff.count But that use, inside the function, the litteral name of the function; and I want no use litteral name (inside) @+ Mic

Re: count items in generator

2006-05-15 Thread Bruno Desthuilliers
George Sakkis a écrit : (snip) > def length(iterable): > try: return len(iterable) > except: except TypeError: > i = 0 > for x in iterable: i += 1 > return i > (snip) -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-15 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: . . . >I'd be a bit worried about having len(x) change x's state into an >unusable one. Yes, it happens in other cases (if y in x:), but adding >more such

Re: count items in generator

2006-05-15 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: >Cameron Laird <[EMAIL PROTECTED]> wrote: > >> In article <[EMAIL PROTECTED]>, >> Alex Martelli <[EMAIL PROTECTED]> wrote: >> . >> . >> . >> >My preference woul

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
George Sakkis wrote: > Delaney, Timothy (Tim) wrote: >> >> list(itertools.count()) will eventually fail with a MemoryError. > > That's more of a theoretical argument on why the latter is worse. How > many real-world programs are prepared for MemoryError every time they > call list(), catch it an

Re: count items in generator

2006-05-14 Thread George Sakkis
Delaney, Timothy (Tim) wrote: > George Sakkis wrote: > > > Paul Rubin wrote: > > > >> [EMAIL PROTECTED] (Cameron Laird) writes: > >>> For that matter, would it be an advantage for len() to operate > >>> on iterables? > >> > >>print len(itertools.count()) > >> > >> Ouch!! > > > > How is this wo

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
Paul Rubin wrote: > That's only because itertools.count itself uses a C int instead of a > long. True. In either case, the effect is the same in terms of whether len(itertools.count()) will ever terminate. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-14 Thread Paul Rubin
"Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > > Actually len(itertools.count()) would as well - when a couple of long > > instances used up everything available - but it would take a *lot* > > longer. > > Actually, this would depend on whether len(iterable) used a C integral > variable to

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
Delaney, Timothy (Tim) wrote: > Actually len(itertools.count()) would as well - when a couple of long > instances used up everything available - but it would take a *lot* > longer. Actually, this would depend on whether len(iterable) used a C integral variable to accumulate the length (which woul

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
George Sakkis wrote: > Paul Rubin wrote: > >> [EMAIL PROTECTED] (Cameron Laird) writes: >>> For that matter, would it be an advantage for len() to operate >>> on iterables? >> >>print len(itertools.count()) >> >> Ouch!! > > How is this worse than list(itertools.count()) ? list(itertools.c

Re: count items in generator

2006-05-14 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > > > [EMAIL PROTECTED] (Cameron Laird) writes: > > > For that matter, would it be an advantage for len() to operate > > > on iterables? > > > >print len(itertools.count()) > > > > Ouch!! > > How is this worse than list(itertools.

Re: count items in generator

2006-05-14 Thread Alex Martelli
Cameron Laird <[EMAIL PROTECTED]> wrote: > In article <[EMAIL PROTECTED]>, > Alex Martelli <[EMAIL PROTECTED]> wrote: > . > . > . > >My preference would be (with the original definition for > >words_of_the_file) to code > > > > nu

Re: count items in generator

2006-05-14 Thread George Sakkis
Paul Rubin wrote: > [EMAIL PROTECTED] (Cameron Laird) writes: > > For that matter, would it be an advantage for len() to operate > > on iterables? > >print len(itertools.count()) > > Ouch!! How is this worse than list(itertools.count()) ? -- http://mail.python.org/mailman/listinfo/python-l

Re: count items in generator

2006-05-14 Thread BartlebyScrivener
>> True. Changing the except clause here to >> except: return sum(1 for x in iterable) >> keeps George's optimization (O(1), not O(N), for containers) and is a >> bit faster (while still O(N)) for non-container iterables. Every thing was going just great. Now I have to think again. Thank you a

Re: count items in generator

2006-05-14 Thread Paul Rubin
[EMAIL PROTECTED] (Cameron Laird) writes: > For that matter, would it be an advantage for len() to operate > on iterables? print len(itertools.count()) Ouch!! -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-14 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: . . . >My preference would be (with the original definition for >words_of_the_file) to code > > numwords = sum(1 for w in words_of_the_file(thefilepath)

Re: count items in generator

2006-05-14 Thread Alex Martelli
Paul Rubin wrote: > "George Sakkis" <[EMAIL PROTECTED]> writes: > > As clunky as it seems, I don't think you can beat it in terms of > > brevity; if you care about memory efficiency though, here's what I use: > > > > def length(iterable): > > try: return len(iterabl

Re: count items in generator

2006-05-13 Thread Paul Rubin
"George Sakkis" <[EMAIL PROTECTED]> writes: > As clunky as it seems, I don't think you can beat it in terms of > brevity; if you care about memory efficiency though, here's what I use: > > def length(iterable): > try: return len(iterable) > except: > i = 0 > for x in iterab

Re: count items in generator

2006-05-13 Thread BartlebyScrivener
Thanks! And thanks for the Cookbook. rd "There is no abstract art. You must always start with something. Afterward you can remove all traces of reality."--Pablo Picasso -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-13 Thread George Sakkis
BartlebyScrivener wrote: > Still new. I am trying to make a simple word count script. > > I found this in the great Python Cookbook, which allows me to process > every word in a file. But how do I use it to count the items generated? > > def words_of_file(thefilepath, line_to_words=str.split): >

Re: count items in generator

2006-05-13 Thread Alex Martelli
BartlebyScrivener <[EMAIL PROTECTED]> wrote: > Still new. I am trying to make a simple word count script. > > I found this in the great Python Cookbook, which allows me to process > every word in a file. But how do I use it to count the items generated? > > def words_of_file(thefilepath, line_to

Re: count string replace occurances

2005-06-13 Thread William Park
Xah Lee <[EMAIL PROTECTED]> wrote: > if i have > mytext.replace(a,b) > how to find out many many occurances has been replaced? If 'a' and 'b' are different length, - Count the string length, before and after. The difference should be multiple of difference between length of 'a' and 'b'.

Re: count string replace occurances

2005-06-12 Thread George Sakkis
"Jeff Epler" wrote: > On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote: > > if i have > > mytext.replace(a,b) > > how to find out many many occurances has been replaced? > > The count isn't returned by the replace method. You'll have to count > and then replace. > > def count_replace(a, b,

Re: count string replace occurances

2005-06-12 Thread Jeff Epler
On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote: > if i have > mytext.replace(a,b) > how to find out many many occurances has been replaced? The count isn't returned by the replace method. You'll have to count and then replace. def count_replace(a, b, c): count = a.count(b) retur

Re: count files in a directory

2005-05-21 Thread Steven Bethard
rbt wrote: > Heiko Wundram wrote: > import os > path = "/home/heiko" > file_count = sum((len(f) for _, _, f in os.walk(path))) > file_count > > Thanks! that works great... is there any significance to the underscores > that you used? I've always used root, dirs, files when using o

Re: count files in a directory

2005-05-21 Thread rbt
James Stroud wrote: > Sorry, I've never used os.walk and didn't realize that it is a generator. > > This will work for your purposes (and seems pretty fast compared to the > alternative): > > file_count = len(os.walk(valid_path).next()[2]) Thanks James... this works *really* well for times when

Re: count files in a directory

2005-05-21 Thread rbt
Heiko Wundram wrote: > Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud: > >>This will work for your purposes (and seems pretty fast compared to the >>alternative): >> >>file_count = len(os.walk(valid_path).next()[2]) > > > But will only work when you're just scanning a single directory with

Re: count files in a directory

2005-05-20 Thread Heiko Wundram
Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud: > This will work for your purposes (and seems pretty fast compared to the > alternative): > > file_count = len(os.walk(valid_path).next()[2]) But will only work when you're just scanning a single directory with no subdirectories...! The altern

Re: count files in a directory

2005-05-20 Thread James Stroud
Sorry, I've never used os.walk and didn't realize that it is a generator. This will work for your purposes (and seems pretty fast compared to the alternative): file_count = len(os.walk(valid_path).next()[2]) The alternative is: import os import os.path file_count = len([f for f in os.listdi

Re: count files in a directory

2005-05-20 Thread James Stroud
Come to think of it file_count = len(os.walk(valid_path)[2]) -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo/python-list

Re: count files in a directory

2005-05-20 Thread James Stroud
On Friday 20 May 2005 07:12 pm, rbt wrote: > I assume that there's a better way than this to count the files in a > directory recursively. Is there??? > > def count_em(valid_path): > x = 0 > for root, dirs, files in os.walk(valid_path): > for f in files: > x = x+1 >