1. 程式人生 > >Python 指令碼定義阿貝爾複雜度

Python 指令碼定義阿貝爾複雜度

def get_set_number(a):  #get Abe_cpx combination not the rank 
    a_2  = []
    for i in range(len(a)):
        a[i] = sorted(a[i])
        a[i] = ''.join(a[i])
        a_2.append(a[i])
    a_2_set = set(a_2)
    a_2_set_length = len(a_2_set)
    return a_2_set_length

def abe_cpx(w):
        empty_list = []
        for j in range(len(w)+1):
                for i in range(len(w)-j+1):
                        empty_list.append(w[i:i+j])
        empty_list = list(filter(None,empty_list))  #filter "" of all examples
        #print(empty_list)
        empty_list_set = set(empty_list)
        empty_list_set = sorted(empty_list_set,key = lambda i:len(i),reverse=False)
        #print(empty_list_set)
        list_num = []
        for i in range(1,len(w)+1):
                list_1 =[]
                for j in range(len(empty_list_set)):
                        if len(empty_list_set[j]) == i:
                                list_1.append(empty_list_set[j])
                list_num.append(list_1)
        #print('####################################################')
        #print('list_num:',list_num)
        Abe_num = []
        for i in range(len(list_num)):
               Abe_num.append(get_set_number(list_num[i])) #the function get the combination   
        return Abe_num
       
print(abe_cpx('INPVQQQN'))

以氨基酸的序列“INPVQQQN”為例,得到阿貝爾複雜度的結果如下: