两个list缩进为一个list,python

# w_list = ['a', 'b', 'c', 'd']
# e_list = ['c', 'd', 'b', 'a']
w_list = ['a', 'b', 'c', 'd', 'ff', 'gg']
e_list = ['c', 'd', 'ff', 'gg', 'b', 'a']
# w_list = ['a', 'b', 'c', 'd']
# e_list = ['c', 'e', 'b', 'a']
# w_list = ['a', 'b', 'c', 'd']
# e_list = ['e', 'b', 'a']

new_list = []
# 先判断e_list 的第一个 元素,是否在w_list 里出现
if e_list[0] in w_list:
    #
    for i, w_port in enumerate(w_list):
        #
        increase_w_index = i
        # 循环w_list ,找到e_list 第一个元素 在 w_list 里出现的位置
        if e_list[0] == w_port:
            # 初始化,切割 下标,切割e_list 使用
            cut_index = 1
            # 在w_list 里 从相同的位置开始 到 w_list 的最后 位置,还剩下几个元素 要循环
            for j in range((len(w_list) - 1) - i):
                #判断 从 第一个 相同 位置开始,两个list同时 往 后走,一一对比后面的元素 是否相同
                if e_list[j + 1] == w_list[increase_w_index + 1]:
                    cut_index += 1
                    increase_w_index += 1
            w_list.extend(e_list[cut_index:len(e_list)])
            new_list = w_list
            break
else:
    new_list = w_list + e_list
print new_list

输出为

  ['a', 'b', 'c', 'd', 'ff', 'gg', 'b', 'a']

猜你喜欢

转载自www.cnblogs.com/angdh/p/10503874.html