Skip to main content
 首页 » 编程设计

python之找到元素的索引在字符串中出现的次数

2024年11月01日23youxin

Char_Record 是一个 3 项列表 [char, total, pos_list] 其中

  • char是一个字符串
  • total 是一个Nat,表示char
  • 出现的次数
  • pos_list 是表示 char
  • 索引的 Nat 列表

使用函数 build_char_records() 应该生成一个排序列表,每个字符都表示(小写)。

例如:

>>>build_char_records('Hi there bye') 
['',2,[2,8]] 
['b',1,[9]] 
['e',3,[5,7,11]] 
['h',2[0,4]] 
['i',1,[1]] 
['r',1,[6]] 
['t',1,[3]] 
['y',1,[10]] 

我就是这么写的,不知道怎么弄,求大神指教。谢谢。

def build_char_records(s): 
    s=sorted(s) 
    a=[] 
    for i in range(len(s)): 

请您参考如下方法:

我认为到目前为止给出的其他答案从整体编程的角度来看是更好的答案,但根据您的问题,我认为这个答案适合您的技能水平

def build_char_records(phrase): 
    phrase = phrase.lower() 
    resultList = [] 
    for character in phrase: ## iterates through the phrase 
        if character not in resultList: 
            resultList.append(character) ## This adds each character to the list 
                                         ## if it is not already in the list         
    resultList.sort() ## sorts the list 
    for i in range(len(resultList)): ## goes through each unique character 
        character = resultList[i] ## the character in question 
        tphrase = phrase ## a copy of the phrase 
        num = phrase.count(character) ## the number of occurences 
        acc = 0 ## an accumulator to keep track of how many we've found 
        locs = [] ## list of the locations 
 
        while acc < num: ## while the number we've found is less than how many 
                         ## there should be 
            index = tphrase.find(character) ## finds the first occurance of the character 
            tphrase = tphrase[index+1:] ## chops off everything up to and including the 
                                        ## character 
            if len(locs) != 0: ## if there is more than one character 
                index = locs[acc-1] + index + 1 ## adjusts because we're cutting up the string 
            locs.append(index)## adds the index to the list 
            acc += 1 ## increases the accumulator 
 
        resultList[i] = [character, num, locs] ## creates the result in the proper spot 
 
    return resultList ## returns the list of lists 
 
print build_char_records('Hi there bye') 

这将打印出 [[' ', 2, [2, 8]], ['b', 1, [9]], ['e', 3, [5, 7, 11] ], ['h', 2, [0, 4]], ['i', 1, [1]], ['r', 1, [6]], ['t', 1, [3] ], ['y', 1, [10]]]

这是一个更短、更清晰的版本

def build_char_records(phrase): 
    phrase = phrase.lower() 
    resultList = [] 
    for character in phrase:  
        if character not in resultList: 
            resultList.append(character)  
 
    resultList.sort()  
    for i in range(len(resultList)):  
        tphrase = phrase  
        num = phrase.count(resultList[i])  
        locs = []  
 
        for j in range(num): 
            index = tphrase.find(resultList[i])  
            tphrase = tphrase[index+1:] 
            if len(locs) != 0: 
                index = locs[acc-1] + index + 1 
            locs.append(index) 
 
        resultList[i] = [resultList[i], num, locs]  
 
    return resultList 
 
print build_char_records('Hi there bye')