# 问题分析

A 是两重列表，我们只要判断列表 A 的第 i 个元素的长度，如果长度等于1，直接添加 B[i] 即可，否则添加长度为 len(A[i]) 的 B 中对应元素构成的列表即可。

``````def copy_struct(lista, listb):
result = []
for i in lista:
if isinstance(i, int):
result.append(listb.pop(0))
else:
result.append([listb.pop(0) for _ in range(len(i))])
return result
``````

``````a = [1,[2,3],[4,5,6],7]
b = [2,3,4,5,6,7,8]
print copy_struct(a, b)
>>> [2, [3, 4], [5, 6, 7], 8] # 实现问题要求
``````

list.pop( )用法

# 问题扩展

``````stra = list_a.__str__().replace(' ','').split(',')
strb = list_b.__str__().replace(' ','').split(',')
``````

``````['[1', '[2', '3]', '[4', '[5', '6]]', '7]']
['[2', '3', '4', '5', '6', '7', '8]']
``````

``````import string
nums = string.digits

def string_number(strrs):
i = 0
for _, strr in enumerate(strrs):
if strr not in nums:
i += 1
return i

def structure_b2a(list_a,list_b):
stra = list_a.__str__().replace(' ','').split(',')
strb = list_b.__str__().replace(' ','').split(',')
for i, r in enumerate(stra):
if r[0]=='[':
strb[i] = '[' * string_number(r) + strb[i]
if r[-1]==']':
strb[i] = strb[i] + ']' * string_number(r)

return eval(strb.__str__()[3:-3].replace("'",''))
``````

# 算法测试

``````A=[1,[2,3],[4,[5,6]],7]
B=[2,3,4,5,6,7,8]
>>> [2, [3, 4], [5, [6, 7]], 8]
``````
``````A=[1,[2,3],[4,[[5,6],7]],8]
B=[2,3,4,5,6,7,8,9]
>>> [2, [3, 4], [5, [[6, 7], 8]], 9]
``````

Stay hungry, Stay foolish -- Steve Jobs.