Merge two or more lists with given order of merging

前端 未结 2 1575
悲哀的现实
悲哀的现实 2020-12-10 16:36

On start I have 2 lists and 1 list that says in what order I should merge those two lists. For example I have first list equal to [a, b, c] and second list equa

2条回答
  •  甜味超标
    2020-12-10 17:13

    How about this,

    list1 = ['a', 'b', 'c']
    list2 = ['d', 'e']
    options = [0,1,0,0,1] 
    
    list1_iterator = iter(list1)
    list2_iterator = iter(list2)
    
    new_list = [next(list2_iterator) if option else next(list1_iterator) for option in options]
    
    print(new_list)
    # Output
    ['a', 'd', 'b', 'c', 'e']
    

提交回复
热议问题