in Education by
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! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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)

Related questions

0 votes
    the following is my question: data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n', ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    the following is my question: data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n', ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    the following is my question: data = ['a1_1 00000001\n', 'a1_2 00000002\n', 'a1_3 00000003\n', ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
+1 vote
    How to find the index of an item in a list in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    How to Accessing the index in 'for' loops in Python?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    In a computational complexity theory, a problem with decision making is said to be NP-complete when it is both ... c) Non-deterministic Polynomial time d) Non Probabilistic time...
asked Dec 29, 2022 in Technology by JackTerrance
0 votes
    Which of the following testing is concerned with making decisions using data? (a) Probability (b) Hypothesis (c ... and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
...