Biotechgeek Biotechgeek - 1 month ago 4
Python Question

How to break down and print two list of lists together in Python?

Here are two lists:

a_value = [[0.234, 0.88,0.98],[0.923,0.777,0.87],[0.77,0.98,0.89]]
b_value = [[(1,1),(1,2),(1,3)],[(1,1),(1,2),(1,3)],[(1,1),(1,2),(1,3)]]


I need to join the two lists such that I have output that prints as:

Set1
b_value (1,1) = a_value 0.234
b_value (1,2) = a_value 0.88
b_value (1,3) = a_value 0.98
Set2
b_value (1,1) = a_value 0.923
b_value (1,2) = a_value 0.777
b_value (1,3) = a_value 0.87
Set 3
b_value (1,1) = a_value 0.77
b_value (1,2) = a_value 0.98
b_value (1,3) = a_value 0.89


The code that I have attempted looks like:

print("\n".join([('b_value{} a_value={}'.format(i,j)) for i,j in zip(b_value,a_value)]))


Output:

b_value[(1, 1), (1, 2), (1, 3)] a_value=[0.234, 0.88, 0.98]
b_value[(1, 1), (1, 2), (1, 3)] a_value=[0.923, 0.777, 0.87]
b_value[(1, 1), (1, 2), (1, 3)] a_value=[0.77, 0.98, 0.89]


I am not sure how to modify the code such that it breaks down the list of lists and also separates them into "Sets".

Answer

Since you only need to print the values and not build a list, you should use for loops instead of the list comprehension. And then you also need to zip the sublists in a nested loop to place their items side-by-side:

for idx, (i ,j) in enumerate(zip(b_value,a_value), 1):
    print("Set{}".format(idx))
    for a,b in zip(i,j):
        print('b_value {} = a_value {}'.format(a,b))

Set1
b_value (1, 1) = a_value 0.234
b_value (1, 2) = a_value 0.88
b_value (1, 3) = a_value 0.98
Set2
b_value (1, 1) = a_value 0.923
b_value (1, 2) = a_value 0.777
b_value (1, 3) = a_value 0.87
Set3
b_value (1, 1) = a_value 0.77
b_value (1, 2) = a_value 0.98
b_value (1, 3) = a_value 0.89