2017-05-26 128 views
0

有一个函数用于将字符串与列表中的项目进行匹配,该列表项目返回列表项目的索引编号是一场比赛。如下图所示:TypeError:在while循环中+:'NoneType'和'int'的不受支持的操作数类型

def get_int(get_wd, get_list): 
    for i, j in enumerate(get_list): 
     if j == get_wd: 
     get_i = i 
     return get_i 

而且还有一个while循环中的主要功能从上面的函数获得返回整数

get_wd = [] 
x = 0 
candi = [] 

while len(li_a) > 0: 
    iter_a = iter(li_a) 
    srh_time = len(li_a) 
    while srh_time > 0: 
     temp = next(iter_a) 
     if temp in li_words: 
      candi.append(temp) 
     else: 
      pass 
     srh_time = srh_time - 1 
    max_len = max(len(s) for s in candi) 
    extr_wd = list(set(s for s in candi if len(s) == max_len)) 
    pos = get_int(extr_wd, li_a) ##Calling the function## 
    get_wd.append(extr_wd) 
    li_a = li_a[pos + 1:] 

我收到此错误信息:

>> li_a = li_a[pos + 1:] 
>> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 

对于我失踪的任何建议?

+0

POS机不等于什么,你上面写的你的函数没有返回任何因此变量分配它为'NoneType'。你的原始函数中也不需要'get_i'。只要返回'我','如果j == get_wd:' –

+0

我也曾尝试过,但仍然有相同的消息。 :( – htetmyet

回答

2

我觉得get_int期待strint为第一list作为第二个参数,但这里pos = get_int(extr_wd, li_a)两个参数都是list,你应该解决这个问题。

您可以使用.index查找索引

重构get_int方法:

def get_int(get_wd, get_list): 
    try: 
     return get_list.index(get_wd) 
    except ValueError: 
     return -1 # not found 
+0

Boom !!,它工作。谢谢:) – htetmyet

相关问题