比较并合并 python 中的两个列表

Compare and Merge two lists in python

我有两个列表。我想合并这两个列表。

list_1 = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i']
list_2 = ['a', 'b', 'd', 'e', 'f', 'h', 'i']

我想要的输出是

['a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i']

我试过下面的算法

missing_elem = []
missing_index = []
elem_to_add = []

for i, elem in enumerate(list_2):
    if elem not in list_1:
        missing_elem.append(elem)
        missing_index.append(i)

print(missing_index)
print(missing_elem)

for i in range(len(missing_index)):
    elem_to_add.append(missing_elem[i])
    list_1.insert(missing_index[i], col_to_add)
    elem_to_add = []
    
print(list_1)

上面的输出是

[3]
['e']
['a', 'b', 'c', ['e'], 'd', 'f', 'g', 'h', 'i']

因为 e 在 list_2 中介于 'd' 和 'f' 之间,但在输出中我得到它在 'c' 和 'd' 之间 请帮助我获得正确的输出

您可以使用集合来获取所有唯一条目,然后按字母顺序对其进行排序。

combined = sorted(list(set(list1 + list2)))

听起来你想要的规则是:

  1. 将第一个列表中的元素添加到最终列表(如果尚不存在)
  2. 如果第二个列表中的元素不存在,则将其添加到最终列表中

如果是这样,算法就非常简单了

list_1 = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i']
list_2 = ['a', 'b', 'd', 'e', 'f', 'h', 'i']

longest_list_len = max(len(list_1), len(list_2))
final_list = []
for i in range(longest_list_len):
    if i < len(list_1):
        elem_1 = list_1[i]
        if elem_1 not in final_list:
            final_list.append(elem_1)
    if i < len(list_2):
        elem_2 = list_2[i]
        if elem_2 not in final_list:
            final_list.append(elem_2)
print(final_list)
> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

这正是您在示例代码中尝试执行的操作,但我不知道它是否符合您最终想要执行的操作:

list_1 = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i']
list_2 = ['a', 'b', 'd', 'e', 'f', 'h', 'i']


for i in range(len(list_2)):
    if list_2[i] not in list_1:
        list_1.insert(i + 1 , list_2[i])

print(list_1)
>>>['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

其他人问的正是您所说的值“应该”去哪里的意思,这对人们来说才有意义。我们知道“e”位于“d”和“f”之间,但计算机不会(当然在某种范围之外)

有很多方法可以告诉计算机列表应该是什么。 你可以有一个主列表:

masterlist = ['a', 'b', 'c', '1', 'e', 'f', 'g', 'h', 'i']

然后将您的列表与您的主列表进行比较。

根据您的努力,这是一个可能的解决方案:

list_1 = ['a', 'c', 'b', 'b2', 'd', 'f', '1g', 'h', 'i']
list_2 = ['e4', 'z', 'a', 'c', 'd', 'code', 'e', 'e2', 'f', 'h', 'i', 'j']

print(list_1)

# Locate the missing elements in list_1 and their left neighbors
missing_elem_and_neighbor = []
for ind, elem in enumerate(list_2):
    if elem not in list_1:
        if ind > 0:
            missing_elem_and_neighbor.append((elem, list_2[ind-1]))
        else:
            missing_elem_and_neighbor.append((elem, None))

# Insert missing elements into list_1
for elem, nei in missing_elem_and_neighbor:
    if nei:
        ind_nei = list_1.index(nei)
        list_1.insert(ind_nei+1, elem)
    else:
        if list_1[0] in list_2:
            # Goes before 0 in list_1
            list_1.insert(0, elem)
        else:
            # Assumption - right after the first in list_1
            list_1.insert(1, elem)

print(list_1)

我使测试用例更具代表性-它不是按字母顺序排列以避免混淆,它具有连续的缺失值,并且它也以缺失值开头。

这段代码没有找到缺失值,而是找到了它们的左邻居。然后它根据 list_1 中左邻居的位置插入缺失值,从而保留顺序。

如果缺失值出现在 list_1 的第一个元素之前,它们将按照它们在 list_2 中出现的顺序插入到 list_1 的开头。如果 list_1 中的第一个元素在 list_2 中完全缺失,那么 list_2 值最终会从 list_1 中的索引 1 插入(检查 list_1 = ['e4', 'z', 'c', 'd', 'code', 'e', 'e2', 'f', 'h', 'i', 'j'])。

为了避免 ValueError 在连续缺失元素的情况下,缺失的元素被直接插入到 list_1 中,从左到右(这也可以用单独的列表来完成,当然,点是从左到右的插入顺序,所以邻居总是存在于合并列表中)。