When you encounter a requirement, you need to cross-merge a list of two lengths that are not necessarily equal. Like a zipper (the zippers on both sides are not necessarily equal).
Such as:
A = [1, 3, 5]
b = [2, 4, 6, 8]
Need to merge A, b into C
c = [1, 2, 3, 4, 6, 8]
See the definition function on the net, or use zip, oneself feel not too ideal, want to daoteng under, of course, the following method may have been thought of.
Method One: Write for loop
A = [1, 3, 5]
b = [2, 4, 6, 8]
c = []
For I in range (max (Len (a), Len (b))):
If a:
C.append (A.pop ())
If B:
C.append (B.pop ())
Method Two: List expression (super long, tried for a long time to get out)
A = [1, 3, 5]
b = [2, 4, 6, 8]
A.reverse ()
B.reverse ()
c = [(Lambda I:a.pop () if (a! = [] and (i% 2 ==0 or b==[])) Else B.pop ()) (i) for I in Range (Len (a) + Len ( b)]
--by Clay
Python list Two unequal-length lists cross merge