简体   繁体   English

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

[英]how to compare the sequence of characters in two strings

Python version is 2.7.9 In python, I have the baseline strings and other strings to be compared: 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

The difflib.SequenceMatcher can not solve my problem. difflib.SequenceMatcher无法解决我的问题。 Need the python guru to give some advice. 需要python guru给出一些建议。 Thanks a lot. 非常感谢。

Try this, using the itertools will help you overcome the strings being of different lengths. 试试这个,使用itertools将帮助你克服不同长度的字符串。 .zip compares character for character in each of the strings and returns them in tuples . .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)
# 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM