Kun
Kun

Reputation: 591

how to print lists separately instead of merging them?

The data, dic and abst are the lists, data in the form of: [[0, 1, 0, 0 , 1...0],[1, 0, 0, 1, 0, 1....0]], after the loop, I got the results like this:

abst = ['PS50802', 'PS50803', 'PS50804', 'PS50805', 'PS50806', 'PS50807', 'PS50808', 'PS50809', 'PS50810', 'PS50811', 'PS50812', 'PS50813', 'PS50814',......]

dic = ['PS50102' 'PS50101' 'PS50106' 'PS50199' 'PS50196' 'PS00832' 'PS50072'...]

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]

Looks like the two individual lists merged into one list, but how can I get the results like this:

[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]]

lines = []

for x in range(0, 2):
    for item in dic:
        for i,j in enumerate(abst):       
            if item == j:
                lines.append(data[x][i])

print lines

Upvotes: 0

Views: 54

Answers (1)

Julien Spronck
Julien Spronck

Reputation: 15433

If you want to keep a list of two lists, you can try this:

lines = []
for x in range(0, 2):
    line = []
    for item in dic:
        for i,j in enumerate(abst):       
            if item == j:
                line.append(data[x][i])
    lines.append(line)

print lines

which you might be able to simplify

lines = []
for x in range(0, 2):
    line = []
    for item in dic:
        if item in abst:
            i = abst.index(item)
            line.append(data[x][i])
    lines.append(line)

or

lines = []
for x in range(0, 2):
    line = [data[x][abst.index(item)] for item in dic if item in abst]
    lines.append(line)

Finally, with one list comprehension:

lines = [ [data[x][abst.index(item)] for item in dic if item in abst] for x in range(0, 2) ]

Upvotes: 1

Related Questions