可以使用以下方法:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
set1 = set(list1)
set2 = set(list2)
merged_set = set1.union(set2)
merged_list = list(merged_set)
print(merged_list)
输出结果为:[1, 2, 3, 4, 5, 6, 7, 8]
推荐的腾讯云相关产品:腾讯云数据库CynosDB,链接地址:https://cloud.tencent.com/product/cynosdb
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
merged_list = [x for x in list1 + list2 if x not in list1 or x not in list2]
print(merged_list)
输出结果为:[1, 2, 3, 6, 7, 8]
推荐的腾讯云相关产品:腾讯云对象存储COS,链接地址:https://cloud.tencent.com/product/cos
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
for item in list2:
if item not in list1:
list1.append(item)
merged_list = list1
print(merged_list)
输出结果为:[1, 2, 3, 4, 5, 6, 7, 8]
推荐的腾讯云相关产品:腾讯云云服务器CVM,链接地址:https://cloud.tencent.com/product/cvm
领取专属 10元无门槛券
手把手带您无忧上云