2014-03-25 26 views
2

Char_Record是3項列表[char, total, pos_list]其中找到元素的索引occurence的字符串數

  • char是一個字符串
  • total位於NAT較char
  • 出現的次數
  • pos_list是NAT的代表char
的索引列表

使用函數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)): 
+0

你也許可以爲每個字符做匹配次數的正則表達式。即「a」:'count = len(re.match(/ [a]/g,phrase).groups())? –

回答

2

我想給其他答案迄今爲止是從整體規劃的角度更好的答案,但根據你的問題,我認爲這個答案是適合你的技能水平

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

在這裏,我s稍短一點,更乾淨的版本

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') 
0
from collections import defaultdict 

def build_char_records(s): 
    cnt = defaultdict(int) 
    positions = defaultdict(list) 
    for i,c in enumerate(s): 
     cnt[c] += 1 
     positions[c].append(i) 
    return [ [c, cnt[c], positions[c]] for c in cnt.keys() ] 
+0

好吧,我不認爲我已經瞭解了收藏和defaultdict.Any其他想法?謝謝。 – user3457749

0

只使用list,這是你可以做什麼:

def build_char_records(s): 
    records = [] # Create a list to act as a dictionary 
    for idx, char in enumerate(s): 
     char = char.lower() 
     current_record = None # Try to find the record in our list of records 
     for record in records: # Iterate over the records 
      if record[0] == char: # We find it! 
       current_record = record # This is the record for current char 
       break # Stop the search 
     if current_record is None: # This means the list does not contain the record for this char yet 
      current_record = [char, 0, []] # Create the record 
      records.append(current_record) # Put the record into the list of records 
     current_record[1] += 1 # Increase the count by one 
     current_record[2].append(idx) # Append the position of the character into the list 

    for value in records: # Iterate over the Char_Record 
     print value # Print the Char_Record 

或者,如果你需要對它進行排序,你可以做什麼@Dannnno說,還是作爲一個例子,它可以按這種方式排序(雖然您可能還沒有了解lambda):

records.sort(key=lambda x: x[0]) 

只是在打印記錄之前。

或者,您可以使用dictlist做到這一點:

def build_char_records(s): 
    record = {} # Create an empty dictionary 
    for idx, char in enumerate(s): # Iterate over the characters of string with the position specified 
     char = char.lower() # Convert the character into lowercase 
     if char not in record: # If we have not seen the character before, create the Char_Record 
      record[char] = [char,0,[]] # Create a Char_Record and put it in the dictionary 
     record[char][1] += 1 # Increase the count by one 
     record[char][2].append(idx) # Append the position of the character into the list 
    for value in record.values(): # Iterate over the Char_Record 
     print value # Print the Char_Record 
+0

我還沒有學過字典。 。 – user3457749

+0

好的,我們可以將字典更改爲列表,將帶有循環的簡單引用更改爲'record [char]'進行搜索。但要對其進行排序,您需要@Dannnno的方法 – justhalf

相關問題