python - find the index of element the number of occurence in string -


a char_record 3 item list [char, total, pos_list]

  • char 1 character string
  • total nat representing number of occurrences of char
  • pos_list list of nat representing indices of char

using function build_char_records() should produce sorted list every character represented (lowercase).

for example:

>>>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]] 

i wrote , don't know how it, please. thanks.

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

i think other answers given far better answers overall programming perspective, based on question think answer appropriate skill level

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

this print out [[' ', 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]]]

here shorter, cleaner version

def build_char_records(phrase):     phrase = phrase.lower()     resultlist = []     character in phrase:          if character not in resultlist:             resultlist.append(character)       resultlist.sort()      in range(len(resultlist)):          tphrase = phrase          num = phrase.count(resultlist[i])          locs = []           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') 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -