如何获得两个字符串列表之间的差异(忽略部分匹配)

时间:2019-04-02 17:37:26

标签: python python-2.7

listA =['set', 'pet', 'get']

listB =['set_tet', 'rgrgrgrg', 'grggrr', 'get']

expected output = ['pet', 'rgrgrgrg', 'grggrr']

'set'部分位于listB中,因此将被忽略。 在listB中找不到“ pet”,因此将其包括在内。 在listB中找到“ get”,因此将其忽略。 不包含“ set_tet”,因为我们之前已经匹配了“ set” 之所以包含“ rgrgrgrg”和“ grggrr”,是因为它与listA中的任何内容都不匹配

如何在Python 2.7中做到这一点?

1 个答案:

答案 0 :(得分:2)

从这个问题来看,您似乎正在寻找listB + listA - intersection of listA and listB,对于相交,没有必要使用完全相同的字符串,也可以是子字符串。

您可以尝试以下方法,我已经在python 2.7.9中尝试过

setA = set(listA)
setB = set(listB)

# union of setA and setB 
AunionB = setA | setB

# intersection of setA and setB
AinterB = setA & setB

# calculate A union B minus A intersection B
result_set = AunionB - AinterB

# convert to list
result_list = list(result_set)

# now here we have the partial search stings as well i.e.set and set_tet
# we have to omit it from final output

# create a list which will hold partial match
partial_match_candidate = []

# search for partial match in result set list
for i in range(len(result_list)-1):
    # iterate from i+1 to len(result_list)
    for j in range(i+1 , len(result_list)):
        # check if there is partial match
        if result_list[i] in result_list[j] or result_list[j] in result_list[i]:
            partial_match_candidate.append(result_list[i])
            partial_match_candidate.append(result_list[j])


# now we have to candidates to remove i.e. set and set_tet
result_list_filtered = [ val for val in result_list if val not in partial_match_candidate ]

明智的做法不是最好的方法,但我希望它能有所帮助。