Reputation: 2009
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!
Upvotes: 0
Views: 132
Reputation: 212905
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)
Upvotes: 5
Reputation: 4983
One way is to store the intermediate result in a variable and then print it out outside of your loop.
E.g. something like (not tested):
candidate_print = {}
for i in candidate:
for j in data:
if i in j.split()[0]:
if not i in candidate_print:
candidate_print[i] = []
candidate_print[i] += [j.split()[1]]
# Now print it out
for i in candidate:
print i, ", ".join(candidate_print[i])
Upvotes: 1