python - How to create element pairs of two lists by iterating through them in parallel? -
i want iterate through 2 lists in parallel , create element pairs follows.
my code:
q_node_roots = ['a', 'b', 'c'] s_node_roots = ['x', 'y', 'z'] q_node_pair, s_node_pair in zip([q_node_roots, q_node_roots[1:]], [s_node_roots, s_node_roots[1:]]): print q_node_pair, s_node_pair
expected output:
['a', 'b'] ['x', 'y'] ['b', 'c'] ['y', 'z']
generated output:
['a', 'b', 'c'] ['x', 'y', 'z'] ['b', 'c'] ['y', 'z']
>>> lis1 = ['a', 'b', 'c'] >>> lis2 = ['x', 'y', 'z'] >>> z1 = zip(lis1,lis1[1:]) #use itertools.izip in py2x memory efficiency >>> z2 = zip(lis2,lis2[1:]) >>> x,y in zip(z1,z2): ... print x,y ('a', 'b') ('x', 'y') ('b', 'c') ('y', 'z')
Comments
Post a Comment