Versi Python adalah 2.7.9 Di python, saya memiliki string dasar dan string lain untuk dibandingkan:

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 tidak dapat menyelesaikan masalah saya. Perlu guru python untuk memberikan beberapa saran. Terima kasih banyak.

0
Cao Jack 1 Juni 2019, 14:02

2 jawaban

Jawaban Terbaik
# 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")
0
Cao Jack 3 Juni 2019, 08:48

Coba ini, menggunakan itertools akan membantu Anda mengatasi string yang panjangnya berbeda. .zip membandingkan karakter untuk karakter di setiap string dan mengembalikannya dalam 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)
0
Alexandre B. 1 Juni 2019, 20:18