Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:31+00:00 2026-06-09T13:05:31+00:00

In direct, my code so far is this : from glob import glob pattern

  • 0

In direct, my code so far is this :

from glob import glob
pattern = "D:\\report\\shakeall\\*.txt"
filelist = glob(pattern)
def countwords(fp):
    with open(fp) as fh:
        return len(fh.read().split())
print "There are" ,sum(map(countwords, filelist)), "words in the files. " "From directory",pattern

I want to add a code that counts unique words from pattern(42 txt files in this path) but I don’t know how. Can anybody help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:05:32+00:00Added an answer on June 9, 2026 at 1:05 pm

    The best way to count objects in Python is to use collections.Counter class, which was created for that purposes. It acts like a Python dict but is a bit easier in use when counting. You can just pass a list of objects and it counts them for you automatically.

    >>> from collections import Counter
    >>> c = Counter(['hello', 'hello', 1])
    >>> print c
    Counter({'hello': 2, 1: 1})
    

    Also Counter has some useful methods like most_common, visit documentation to learn more.

    One method of Counter class that can also be very useful is update method. After you’ve instantiated Counter by passing a list of objects, you can do the same using update method and it will continue counting without dropping old counters for objects:

    >>> from collections import Counter
    >>> c = Counter(['hello', 'hello', 1])
    >>> print c
    Counter({'hello': 2, 1: 1})
    >>> c.update(['hello'])
    >>> print c
    Counter({'hello': 3, 1: 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to use direct store in Extjs heres the code to my store
Is there a direct analog to this in C++? for num in [1,4,5] :
This is a very direct follow-up on this question . Using matplotlib , I'd
First point: I'm not a C++ expert, far from it. I glanced at it
The following code works fine so far. But now I want to map the
I have this postgres sql query: select * from stats where athlete_id = 5
C++/CX seems to make interfacing native C++ with C# FAR easier and more direct
I'm currently in need of a purely managed code DirectX wrapper for .NET. While
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
direct console to ok.log, and errors to error.log for example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.