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 7847513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:55:23+00:00 2026-06-02T17:55:23+00:00

I hope I’m able to explain this problem clearly. I’m a python experimenter (just

  • 0

I hope I’m able to explain this problem clearly. I’m a python experimenter (just in case the below query appears naive)

Assume that I have a dataset of the form:

a = ( ('309','308','308'), ('309','308','307'), ('308', '309','306', '304'))

Let me call each ('309','308','308') as a path.

I want to find the count of:

a. Count('309','308', <any word>)

b. Count('309',<any word>,'308')

and all possible permutations.

I’m thinking its some kind of a regex which will help me achieve this search. And, the number of paths I have goes onto 50000.

Can anyone suggest how I can do this kind of an operation in python? I explored trie, radix but I dont think that’ll help me.

Thanks,
Sagar

  • 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-02T17:55:25+00:00Added an answer on June 2, 2026 at 5:55 pm

    You could use collections.Counter to do this:

    >>> from collections import Counter
    >>> a = ( ('309','308','308'), ('309','308','307'), ('308', '309','306', '304'))
    >>> Counter((x, y) for (x, y, *z) in a)
    Counter({('309', '308'): 2, ('308', '309'): 1})
    >>> Counter((x, z) for (x, y, z, *w) in a)
    Counter({('308', '306'): 1, ('309', '308'): 1, ('309', '307'): 1})
    

    I’m also using extended tuple unpacking here, which didn’t exist pre-Python 3.x, which is only needed if you have tuples of an uncertain length. In python 2.x, you could instead do:

    Counter((item[0], item[1]) for item in a)
    

    I couldn’t say how efficient this would be, however. I don’t believe it should be bad.

    A Counter has a dict-like syntax:

    >>> count = Counter((x, y) for (x, y, *z) in a)
    >>> count['309', '308']
    2
    

    Edit: You mentioned they might be of any length greater than one, in this case, you could run into problems as they won’t be able to unpack if they are shorter than the required length. The solution is to change the generator expression to ignore any not in the required format:

    Counter((item[0], item[1]) for item in a if len(item) >= 2)
    

    E.g:

    >>> a = ( ('309',), ('309','308','308'), ('309','308','307'), ('308', '309','306', '304'))
    >>> Counter((x, y) for (x, y, *z) in a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.2/collections.py", line 460, in __init__
        self.update(iterable, **kwds)
      File "/usr/lib/python3.2/collections.py", line 540, in update
        _count_elements(self, iterable)
      File "<stdin>", line 1, in <genexpr>
    ValueError: need more than 1 value to unpack
    >>> Counter((item[0], item[1]) for item in a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.2/collections.py", line 460, in __init__
        self.update(iterable, **kwds)
      File "/usr/lib/python3.2/collections.py", line 540, in update
        _count_elements(self, iterable)
      File "<stdin>", line 1, in <genexpr>
    IndexError: tuple index out of range
    >>> Counter((item[0], item[1]) for item in a if len(item) >= 2)
    Counter({('309', '308'): 2, ('308', '309'): 1})
    

    If you need to have a variable length count, the easiest way is to use a list slice:

    start = 0
    end = 2
    Counter(item[start:end] for item in a if len(item) >= start+end)
    

    Of course, this only works for continuous runs, if you want to pick columns individually, you have to do a little more work:

    def pick(seq, indices):
        return tuple([seq[i] for i in indices])
    
    columns = [1, 3]
    maximum = max(columns)
    Counter(pick(item, columns) for item in a if len(item) > maximum)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope to get solution to this problem. I have been stuck on it since
Hope this is not a dupe. I would like to be able to do
hope this is something simple and I am just missing it, but consider this
Hope I can explain this somewhat decently, as it's blowing a fuse in my
Hope I'm asking this correctly: I have a project Projects.Client I have my class
Hope this is the right place to be asking this, so my apologies if
Hope this isn't a waste of your time. I'm working on a project, and
Hope this makes sense... Is there a simple way to return a set of
Hope someone has an easy answer on this. I have a header image which
Hope this makes sense, I am using Titanium mobile to build an iPhone app.

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.