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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:54:30+00:00 2026-05-27T16:54:30+00:00

the following is my question: data = [‘a1_1 00000001\n’, ‘a1_2 00000002\n’, ‘a1_3 00000003\n’, ‘b1_1

  • 0

the following is my question:

data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n',
        'b1_1 00000004\n', 'b1_2 00000005\n', 'b1_3 00000006']

candidate = ['a', 'b']

for i in candidate:
    for j in data:
        if i in j.split()[0]:
            print i, j.split()[1]

a 00000001
a 00000002
a 00000003
b 00000004
b 00000005
b 00000006

But what I want to do is to make the result like the following:

a 00000001, 00000002, 00000003
b 00000004, 00000005, 00000006

How do I solve this problem?
Thanks in advance!

  • 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-05-27T16:54:31+00:00Added an answer on May 27, 2026 at 4:54 pm
    data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n', 'b1_1 00000004\n', 'b1_2 00000005\n', 'b1_3 00000006']
    
    candidate = ['a', 'b']
    
    
    for i in candidate:
        print i, ', '.join(v for k,v in (a.split() for a in data) if k.startswith(i))
    

    prints:

    a 00000001, 00000002, 00000003
    b 00000004, 00000005, 00000006
    

    This will, however, go through data several times (once for each candidate). To do it once only, you will need an intermediary dictionary:

    d = {}
    for a in data:
        d.setdefault(a[0], []).append(a.split()[1])
    for k,v in sorted(d.iteritems()):
        print k, ', '.join(v)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered the following interview question on the Internet. Describe a data structure for
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
I have a situation similar to the following question: Insert Data Into SQL Table
I have the following question regarding Flex/AIR data grids: Can I access the value
I shortly asked the following question: Keep rows of data frame that match certain
I was asked the following Question: How would you store the data given below(which
I was reading the following question: How to read MNIST data in C++? and
I have following question. I'd like to place a file named data.xml into sdcard/appname
I'm just studying for my data structures & algorithms final. The following question was

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.