2012-04-15 51 views
0

我需要一個函數的幫助,該函數可以返回具有3個或多個「均勻」間隔字符的字,即從左到右連續字母的ord()值是偶數(相同的差值)。這是我迄今爲止...和輸出是這樣的:如何從元組列表(字,值)返回單個元組(單詞,值)的列表?

test_list2 = ['i', 'made', 'an', 'ace', 'today', 'at', 'tennis...yeaah', 'booi', ':d'] 

for word in test_list2: 
    if len(word) >=3: 
    temp_list = [] 
    for chr1 in word: 
    if word.index(chr1) != (len(word)-1): 
     chr2 = word.index(chr1)+1 
     num = ord(word[chr2]) - ord(chr1) 
     temp_list.append(num) 
    temp_tup = (word, temp_list) 
    final_list.append(temp_tup) 

final_list = [('made', [-12, 3, 1]), ('ace', [2, 2]), ('today', [-5, -11, -3, 24]), 
    ('tennis...yeaah', [-15, 9, 0, 0, 10, -69, 0, 0, 0, -20, 9, 0, 0]), 
    ('booi', [13, 0, 0])] 

,但我需要只返回間隔均勻(「王牌」)中的那些。輸出應該是這樣的,

[('ace',2)] 

回答

0

假設你不需要final_list與非均勻間隔的號碼,那麼你就可以保持num的軌跡,看它是否保持整個字相同。如果您發現不同的num停止並轉到下一個單詞。如果num保持不變然後加上一個(word, num)元組到final_list

for word in test_list2: 
    if len(word) >=3: 
    all_nums_are_same = True 
    prev_num = None 
    for chr1 in word: 
     if word.index(chr1) != (len(word)-1): 
     chr2 = word.index(chr1)+1 
     num = ord(word[chr2]) - ord(chr1) 
     if not prev_num: 
      prev_num = num 
     elif prev_num != num: 
      # different number is found, we can 
      # stop and move on to next word 
      all_nums_are_same = False 
      break 

    if all_nums_are_same: 
     # only add tuple if all numbers we the same 
     temp_tup = (word, prev_num) 
     final_list.append(temp_tup) 

這就產生[('ace',2)]結果。

0

我在Python 3.3撞了這一點,編譯和作品在我的機器:)

如果你想要一些更復雜的數據來測試它那裏面有一堆額外的調試垃圾像打印語句,(例如在:長文本塊)的錯誤。

我利用了enumerate(),而不是你的word.index,不知道哪個更pythonic?

import sys 

### Define variables 
test_list = ['i', 'made', 'an', 'ace', 'today', 'at', 'tennis...yeaah', 'booi', ':d'] 
proc_check = [('made', [-12, 3, 1]), 
        ('ace', [2, 2]), 
        ('today', [-5, -11, -3, 24]), 
        ('tennis...yeaah', [-15, 9, 0, 0, 10, -69, 0, 0, 0, -20, 9, 0, 0]), 
        ('booi', [13, 0, 0])] 
final_check = [('ace', [2,2])] 

test_list2 = ['ace', 'ace', 'ace'] 
proc_check2 = [('ace', [2, 2]), 
       ('poo', [3, 3]), 
       ('ace', [2, 2])] 
final_check2 = [('ace', [2,2]),('poo', [2,2]),('ace', [2,2])] 

### Function definitions 
def wordIsEven(word_list, proc_list_check):  
    final_list = [] 
    procd_list = [] 

    for word in word_list: 
     temp_list = [] 

     if len(word) >= 3:   
      for chr1 in word: 
       if word.index(chr1) != (len(word)-1): 
        chr2 = word.index(chr1)+1 
        num = ord(word[chr2]) - ord(chr1) 
        temp_list.append(num) 

      temp_tup = (word, temp_list) 
      procd_list.append(temp_tup) 

    errors = False 
    for i, check in enumerate(procd_list): 
     if check != proc_list_check[i]: 
      errors = True 
      print("Word Eval Fail! " + str(check) + " != " + str(proc_list_check[i])) 

    if errors == True: 
     print("List compare failed!") 
    else: 
     print("Lists compare equally!") 

    for tmp_tup in procd_list: 
     print("Examining Slice: "+str(tmp_tup[1])) 
     for i, num in enumerate(tmp_tup[1]): 
      if i + 1 < len(tmp_tup[1]): 
       num2 = tmp_tup[1][i+1] 

       if num == num2: 
        if num != 0: 
         print("Got one! " + str(tmp_tup)) 
         final_list.append(tmp_tup)    
    return final_list 

### Code execution 

my_list = wordIsEven(test_list2, proc_check2) 
my_check = final_check2 

print("Printing Final list:") 
for i, item in enumerate(my_list): 
    tempStr = str(item) 
    if item != my_check[i]: 
     tempStr += " doesn't match check data..." + str(my_check[i]) 
    print(tempStr) 
sys.exit()