比较两个python列表并将较短的列表扩展为较长列表的长度

Wil*_*Mak 0 python list

我的问题标题有点令人困惑,我只是不确定如何只用标题来解释它.

我有两个清单.

list_1 = [10,20,30,40,50,60,70,80,90]
list_2 = [10,40,70]
Run Code Online (Sandbox Code Playgroud)

预期产量:

new_list = [10,0,0,40,0,0,70,0,0]
Run Code Online (Sandbox Code Playgroud)

我该怎么做?以下是我所拥有的,我不确定是什么问题:

def expand_list(complete_list, to_be_expand_list):
    expanded_list = []
    for i in complete_list:
        for j in to_be_expand_list:
            if i == j:
                expanded_list.append(j)
            else:
                if expanded_list[-1] != 0:
                    expanded_list.append(0)

    return expanded_list
Run Code Online (Sandbox Code Playgroud)

Kwa*_*rtz 6

尝试这样的事情:

def expand_list(full_list, short_list):
  return [x if x in short_list else 0 for x in full_list]
Run Code Online (Sandbox Code Playgroud)

这使用列表推导来生成一个列表,该列表是完整列表的长度,但仅包含短列表中的那些元素,用零替换所有其余元素.