def intercalate_2lists(l1, l2, s1=0, s2=1, app=nan, incr=1, if_print= False):
"""
Usage: Intercalate two lists: L1 and L2 into a new list L3
S1 and S2 are indices in L3 where interacalation of L1 or L2 will start.
INCR is the number of empty elements APP added to L3 at each iteration,
such iteration can be empty if current L3 index is inferior to min(S1, S2).
Intercalation in L3 starts at min(S1,S2) and the first element of the corresponding list is used.
Intercalation of the list corresponding to max(S1, S2) starts once the iteration through L3 reach the index max(S1, S2)
if S1>0 and S2>0: L3 is appended with APP. until iteration of L3 reach min(S1, S2)
if S1 == S2: S2 is incremented by 1 (user should avoid this situation)
S1, S2 can be used to swap the order of integration of L1 and L2, i.e. id S2<S1: L2 will be used first
Parameters:
L1,L2 (list)(list) lists to intercalate
S1,S2 (int),(int) indices in L3 to start to intercalate L1 and L2
INCR (int) step of increment of L3 (spacing between L1&L2 elements)
APP (var) element to put in empty cells of L3
IF_PRINT (bool) Track assembly of L3. Use only for short L3 lists otherwise be ready to saturate your screen with printed output
Return:
L3 list
"""
len_l1 = len(l1)
len_l2 = len(l2)
result = list()
t1,t2 = False, False
d1,d2 = False, False
f1,f2 = True, True
if s1 == s2: s2 += 1 # SAFEGUARD
if incr == 0: incr=1 # SAFEGUARD
c, c1, c2 = 0, 0 ,0
while c1 < len_l1 or c2 < len_l2:
for x in range(incr):
result.append(app)
if all([c >= s1, f1]): t1 = True; d1 = True; f1 = False; d2=False
elif all([c >= s2, f2]): t2 = True; d2 = True; f2 = False; d1=False
if if_print: print("beg:" + str(c) + '\t' + str(c1) + '\t' + str(c2), d1, d2)
if t1:
if d1:
result[-1] = l1[c1]
c1 += 1
if t2:
if c2 < len_l2: d1 = False; d2 = True
if if_print: print("end:" + str(c) + '\t' + str(c1) + '\t' + str(c2), d1, d2)
if c1 == len_l1: t1 = False
c += incr
if if_print: print(result,'___')
continue
if t2:
if d2:
result[-1] = l2[c2]
c2 += 1
if t1:
if c1 < len_l1: d1=True; d2 = False
if if_print: print("end:" + str(c) + '\t' + str(c1) + '\t' + str(c2), d1, d2)
if c1 >= len_l1: t1 = False
if c2 >= len_l2: t2 = False
c += incr
if if_print: print(result)
return result
intercalate_2lists() 的应用:
a = ["A", "B", "C", "D", "E", "F", "G"]
b = [1, 2, 3, 4]
# Use the default behavior
print("case1:", intercalate_2lists(a[:4], b))
print("case2:", intercalate_2lists(a, b))
print("case3:", intercalate_2lists(b, a))
# Use advanced modes
print("case4:", intercalate_2lists(a, b, s1=4, s2=2))
print("case5:", intercalate_2lists(b, a, s1=3, s2=10, incr=3, if_print=0))
print("case6:", intercalate_2lists(a[:4], b, s1=2, incr=1, if_print=0))
上述调用的结果:
case1:['A', 1, 'B', 2, 'C', 3, 'D', 4]
case2:['A', 1, 'B', 2, 'C', 3, 'D', 4, 'E', 'F', 'G']
case3:[1, 'A', 2, 'B', 3, 'C', 4, 'D', 'E', 'F', 'G']
case4:[nan, nan, 1, 2, 'A', 3, 'B', 4, 'C', 'D', 'E', 'F', 'G']
case5:[nan, nan, nan, nan, nan, 1, nan, nan, 2, nan, nan, 3, nan, nan, 'A', nan, nan, 4, nan, nan, 'B', nan, nan, 'C', nan, nan, 'D', nan, nan, 'E', nan, nan, 'F', nan, nan, 'G']
case6:[nan, 1, 'A', 2, 'B', 3, 'C', 4, 'D']
由于这篇文章中的大多数答案只处理大小相等的列表,因此这是一种能够处理不同大小列表的艺术方法。