Convert alphabet letters to number in Python

后端 未结 16 1819
慢半拍i
慢半拍i 2020-11-28 05:09

How can the following be finished?

characters = [\'a\'\'b\'\'c\'\'d\'\'e\'\'f\'\'g\'\'h\'\'i\'\'j\'\'k\'\'l\'\'m\'\'n\'\'o\'\'p\'\'q\'\'r\'\'t\'\'u\'\'v\'\'w         


        
16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 05:53

    Use this function. It converts a string of alphabet to its equivalent digit value:

    def convAlph2Num(sent):
        alphArray = list(string.ascii_lowercase)
        alphSet = set(alphArray)
        sentArray = list(sent.lower())
        x = []
        for u in sentArray:
            if u in alphSet:
                u = alphArray.index(u) + 1
                x.append(u)
        print(x)
        return
    

提交回复
热议问题