以下是一个比较两个Python列表并创建一个新的组合列表的示例代码:
def combine_lists(list1, list2):
combined_list = list1 + list2
return combined_list
# 两个示例列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 调用函数,比较列表并创建新的组合列表
combined_list = combine_lists(list1, list2)
# 打印结果
print(combined_list)
上述代码定义了一个名为combine_lists
的函数,该函数接受两个列表作为参数,并使用+
运算符将两个列表连接起来,然后返回合并后的列表。
在示例代码中,我们定义了两个示例列表list1
和list2
,然后调用combine_lists
函数将它们进行比较并创建新的组合列表。最后,我们打印出合并后的列表结果。
输出结果为:[1, 2, 3, 4, 5, 6]
,即将两个列表按顺序组合起来形成了一个新的列表。