Reputation: 2429
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".
Upvotes: 0
Views: 614
Reputation: 78546
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
Upvotes: 4
Reputation: 51
I don't know how general you want this code, but this prints out want you want I think:
for myset in zip(a_value,b_value):
for i in range(len(myset) + 1):
print("b_value " + str(myset[1][i]) + " = a_value " + str(myset[0][i]))
Upvotes: 1
Reputation: 4730
You need to flatten the two lists, if you want to do this in a single line I'd use itertools.chain
:
from itertools import chain
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)]]
print("\n".join([('b_value{} a_value={}'.format(i,j)) for i,j in zip(chain.from_iterable(b_value),chain.from_iterable(a_value))]))
However, if you need a faster method of flattening, use a few extra lines:
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)]]
flat_a = [item for sublist in a_value for item in sublist]
flat_b = [item for sublist in b_value for item in sublist]
print("\n".join([('b_value{} a_value={}'.format(i,j)) for i,j in zip(flat_b,flat_a)]))
Upvotes: 4
Reputation: 5115
It seems you have the formatting part ok, but having trouble combining the lists correctly. Why not flatten them first?
a = [[0.234, 0.88,0.98],[0.923,0.777,0.87],[0.77,0.98,0.89]]
b = [[(1,1),(1,2),(1,3)],[(1,1),(1,2),(1,3)],[(1,1),(1,2),(1,3)]]
flat_a = [item for sublist in a for item in sublist]
flat_b = [item for sublist in b for item in sublist]
zip(flat_a, flat_b)
Upvotes: 2