Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79168 > unrolled thread

Creating a counter

Started byShiva <shivaji_tn@yahoo.com>
First post2014-10-15 18:39 +0000
Last post2014-10-16 07:12 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Creating a counter Shiva <shivaji_tn@yahoo.com> - 2014-10-15 18:39 +0000
    Re: Creating a counter Simon Kennedy <sffjunkie@gmail.com> - 2014-10-16 06:44 -0700
      Re: Creating a counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-16 08:04 -0600
        Re: Creating a counter Simon Kennedy <sffjunkie@gmail.com> - 2014-10-16 07:12 -0700

#79168 — Creating a counter

FromShiva <shivaji_tn@yahoo.com>
Date2014-10-15 18:39 +0000
SubjectCreating a counter
Message-ID<mailman.14888.1413398369.18130.python-list@python.org>
Hi,

I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.

How do I implement it?

If I use a print() within a if condition statement - and execute the script
in terminal - for each find, the print() prints in new line instead of a
constantly incrementing counter.

Thanks,
Shiva

[toc] | [next] | [standalone]


#79210

FromSimon Kennedy <sffjunkie@gmail.com>
Date2014-10-16 06:44 -0700
Message-ID<7490ed54-3116-45da-85d1-f9ce4bf29c5e@googlegroups.com>
In reply to#79168
On Wednesday, 15 October 2014 19:39:43 UTC+1, Shiva  wrote:
> I am trying to search a string through files in a directory - however while
> Python script works on it and writes a log - I want to present the user with
> count of number of strings found. So it should increment for each string found.

You may ignore the following if you are just starting out on your Python learning odyssey but if you're past the early stages and looking to try something different then read on.

When you looked through the other answers and found a solution you're happy with that does not use the standard library you can look through the documentation and find a stdlib (https://docs.python.org/2.7/library/index.html) provided way that may be slightly more elegant for counting strings.

Something like the following ::

    >>> from collections import defaultdict
    >>> counter = defaultdict(int)
    >>> counter['string1'] += 1
    >>> counter['string2'] += 1
    >>> counter['string1'] += 1
    >>> print('Count string1={string1}, string2={string2}'.format(**counter), end='\r')
    Count string1=2, string2=1

https://docs.python.org/3/library/collections.html#collections.defaultdict

In the above code defaultdict is like a dictionary but if a key is not found then it uses the `int` factory parameter to add a value that is an integer with a value of 0  (which is what executing ``int()`` returns if you do it in the interactive Python prompt) for the dictionary key you're trying to access.

or ::

    >>> from collections import Counter
    >>> counter = Counter()
    >>> s = 'string1 string2 string1'
    >>> s.split()
    ['string1', 'string2', 'string1']
    >>> counter.update(s.split())
    >>> print('Count string1={string1}, string2={string2}'.format(**counter), end='\r')
    Count string1=2, string2=1

https://docs.python.org/2.7/library/collections.html#collections.Counter

[toc] | [prev] | [next] | [standalone]


#79212

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-16 08:04 -0600
Message-ID<mailman.14924.1413468316.18130.python-list@python.org>
In reply to#79210
On Thu, Oct 16, 2014 at 7:44 AM, Simon Kennedy <sffjunkie@gmail.com> wrote:
> When you looked through the other answers and found a solution you're happy with that does not use the standard library you can look through the documentation and find a stdlib (https://docs.python.org/2.7/library/index.html) provided way that may be slightly more elegant for counting strings.

I would have suggested a Counter if I thought it fit the OP's use
case. If you're listing directory contents, you're not going to have
any repeated strings, so all the counts will be 1, and your Counter
might as well be a list, which is what you got from calling
os.listdir() in the first place. So I was more inclined to think that
the OP was only trying to count one thing.

In any case, the question turned out to actually be about printing,
not counting.

[toc] | [prev] | [next] | [standalone]


#79214

FromSimon Kennedy <sffjunkie@gmail.com>
Date2014-10-16 07:12 -0700
Message-ID<6bb7675c-e821-4a10-84c2-582197afd237@googlegroups.com>
In reply to#79212
On Thursday, 16 October 2014 15:05:47 UTC+1, Ian  wrote:
> I would have suggested a Counter if I thought it fit the OP's use
> case. If you're listing directory contents, you're not going to have
> any repeated strings, so all the counts will be 1, and your Counter
> might as well be a list, which is what you got from calling
> os.listdir() in the first place. So I was more inclined to think that
> the OP was only trying to count one thing.

I read the OP's question as he was looking for the string in the contents of each file in the directory not in the file names themselves but as you say...

> In any case, the question turned out to actually be about printing,
> not counting.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web