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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:42:18+00:00 2026-06-06T23:42:18+00:00

I have the following tuple.I want to build a string which outputs as stated

  • 0

I have the following tuple.I want to build a string which outputs as stated in output.I want count all the elements corresponding to ‘a’ i.e, how many k1 occured w.r.t ‘a’ and so on .What is the easiest way to do this

a=[('a','k1'),('b','k2'),('a','k2'),('a','k1'),('b','k2'),('a','k1'),('b','k2'),('c','k3'),('c','k4')]

Output should be in a string output=””

 a k1  3
 a k2  1
 b k1  1
 b k2  3
 c k3  1
 c k4  1
  • 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-06T23:42:19+00:00Added an answer on June 6, 2026 at 11:42 pm

    You can do the addition portion easily with defaultdict. The default dict works like a normal dictionary, except it has a default value for empty key stores so you can easily increment your counter when you iterate over your data set.

    a=[('a','k1'),('b','k2'),('a','k2'),('a','k1'),('b','k2'),('a','k1'),('b','k2'),('c','k3'),('c','k4')]
    from collections import defaultdict
    b = defaultdict(int)
    for item in a:
        b[item] += 1
    
    print b
    defaultdict(<type 'int'>, {('a', 'k2'): 1, ('c', 'k3'): 1, ('b', 'k2'): 3, ('a', 'k1'): 3, ('c', 'k4'): 1})
    

    And for pretty printing it, just iterate over the resulting data and print it how you want.

    for key, value in b.iteritems():
        print '%s %s %s' % (key[0], key[1], value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an (int * string) tuple which I want to covert into a
I have following code: class TR_AgentInfo : public tuple< long long, //AgentId string, //AgentIp
I have a list in the following format: [(index, count, color),(index+1,newcount,othercolor),...] And I want,
I have a tuple called values which contains the following: ('275', '54000', '0.0', '5000.0',
I have list of 10 elements having a tuple of 2 elements I want
I have the following code: foreach (Tuple<Point, Point> pair in pointsCollection) { var points
I have a 3-tuple list like the following [I added line breaks for readability]:
I have following script that executes all the .reg files in the current directory.
I have the following Python list (can also be a tuple): myList = ['foo',
Let's suppose that I have a list of elements, and I want to select

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.