功能函数
def fill_list(my_list: list, length, fill=None): # 使用 fill字符/数字 填充,使得最后的长度为 length
if len(my_list) >= length:
return my_list
else:
return my_list + (length - len(my_list)) * [fill]
用法
def fill_list(my_list: list, length, fill=None):
if len(my_list) >= length:
return my_list
else:
return my_list + (length - len(my_list)) * [fill]
if __name__ == '__main__':
test_list = [1, 2, 3, None, None, 3]
test_list = fill_list(test_list, 10, None)
print(test_list) # [1, 2, 3, None, None, 3, None, None, None, None]