首页 > 解决方案 > Python用不同列表中的值替换列表中的空字符串

问题描述

我正在寻找一种巧妙/优雅的方式,可以用" "其他列表中的值替换一个列表,即:

empty_list = [" ", " ", " "]
other_list = ["a", "b"]
result_list = ["a", "b", " "]

PS我不是在寻找for循环解决方案

标签: python

解决方案


new_list = [ a if b == ' ' else b for a,b in zip( other_list, empty_list)]
new_list += empty_list[len(other_list):]

推荐阅读