打印出字母构成一样的词

primes =  [2, 3, 5, 7, 11 ,13 ,17 ,19 ,23 ,29 ,31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]

sample = ['hat', 'top', 'pot', 'peer', 'pper', 'pire', 'ripe']

def printSameChar(word_lst):
    words_hash_dic = {}
    for word in word_lst:
        w_h = 1
        for c in word:
            w_h *= primes[ ord(c)- 97 ]
        if w_h in words_hash_dic:
            words_hash_dic[w_h].append(word)
        else:
            words_hash_dic[w_h] = [word]

    for x in words_hash_dic:
        print words_hash_dic[x]

printSameChar(sample)

result:

['peer']
['hat']
['pire', 'ripe']
['top', 'pot']
['pper']



你可能感兴趣的:(打印出字母构成一样的词)