I often need to merge two lists like the following:
A={{0.1},{0.2},…,{1}}
B={{x1},{x21,x22,x23},{x31,x32,x33},…,xN}
in a way to ideally obtain a new list such as:
C={{0.1,x1},{0.2,x21},{0.2,x22},{0.2,x23},{0.3,x31},{0.3,x32},{0.3,x33},…,{1,xN}}
At present I do this by inspecting the lists and manually associating them in the appropriate way. In above example, I would do
Table[{A[[k]],B[[k,1]]},{k,1,N}]
Table[{A[[k]],B[[k,2]]},{k,2,S}]
Table[{A[[k]],B[[k,3]]},{k,2,S}]
where S
is the position of the last element that contains three entries in the B list.
These lists are typically very long, and I wonder whether there is a way to do this efficiently.
Comments
Post a Comment