如何比较两个字符串中的字符序列

时间:2019-06-01 11:02:32

标签: python

Python版本为2.7.9 在python中,我有基线字符串和其他要比较的字符串:

baseline_string="c1,c2,c3,c4,c5,c6,c7,c8,c9,c10"
#all the characters appeared as the sequence defined in base line string ,this is ok
compared1="c1,c2,c3,c4,c5,c6,c7,c8,c9,c10" 
#all the characters appeared as the sequence defined in base line string,some charaters don't appear such as c2 and c7 don't appear ,this is ok
compared2="c1,c3,c4,c5,c6,c8,c9,c10"
#some character does not appear as the defind sequece,such as c4 is before c3 ,and c110 is before c9

difflib.SequenceMatcher无法解决我的问题。 需要python大师提供一些建议。 非常感谢。

2 个答案:

答案 0 :(得分:0)

尝试一下,使用 itertools 可以帮助您克服长度不同的字符串。 .zip比较每个字符串中的字符,然后在tuples中返回它们。

import itertools

compared1 = "c1,c2,c3,c4,c5,c6,c7,c8,c9,c10" 

compared2 = "c1,c3,c4,c5,c6,c8,c9,c10"

words = itertools.zip_longest(compared1,compared2,fillvalue=None)

incorrect = len([c for c,d in words if c!=d]) 

print(incorrect)

答案 1 :(得分:0)

# the following code is run ok,but I think that should be better
#method to check
def check_order():
    from collections import OrderedDict
    compared1 = "c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,"
    compared2 = "c1,c3,c4,c5,c6,c8,c9,c10"
    compared2 = "c3,c4,c6,c5,c9,c10,c7"
    compared3 = "c4,c3,c6,c5,c8,c9,c10"
    base_dict = OrderedDict()
    base_order = 1
    compared_dict2 = OrderedDict()
    compared_order2 = OrderedDict()
    compare_order2 = 1

    compared_dict3 = OrderedDict()
    compare_order3 = 1
    check_result = True

    for base_substr in compared1.split(','):
        base_dict[base_substr] = base_order
        base_order = base_order + 1

    for base_keys, base_value in base_dict.items():
        print("key is " + str(base_keys) + \
              " order is " + str(base_value))

    for compared_substr2 in compared2.split(','):
        compared_dict2[compared_substr2] = compare_order2
        compared_order2[compare_order2] = compared_substr2
        compare_order2 = compare_order2 + 1

    for compare_key2, compared_value2 in compared_dict2.items():
        print("key is " + str(compare_key2) +\
              " order is " + str(compared_value2))

    compare_len_2 = len(compared_dict2)
    compare_key_tmp = None
    compare_key_order_in_base = 0
    for compare_key2, compared_value2 in compared_dict2.items():
        based_value = base_dict[compare_key2]
        for num in range(1, compared_value2):
            compare_key_tmp = compared_order2[num]
            compare_key_order_in_base = base_dict[compare_key_tmp]
            print("The key :" + str(compare_key_tmp) +\
                  "and order in base is:" + str(compare_key_order_in_base))
            if compare_key_order_in_base <= based_value:
                print("The key " + str(compare_key_tmp) + \
                      "is less than" + str(based_value))
            else:
                print("Error,The key " + str(compare_key_tmp) +\
                      "is larger than" + str(based_value))
                check_result = False

    return check_result


if __name__ == '__main__':
    check_result = check_order()
    if check_result:
        print("Pass")
    else:
        print("Failed")