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')。输出应该是这样的,

[('ace',2)] 
4

2 回答 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()
于 2012-04-15T06:57:07.473 回答
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)]就是这样。

于 2012-04-15T05:02:15.380 回答