2012-04-15 56 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() 
相关问题