TypeError:在调用函数时,必须是str,而不是Python 3中的列表

问题描述:

我创建了一个函数来计算字母e,例如字母e。我的功能类似于这样的东西:TypeError:在调用函数时,必须是str,而不是Python 3中的列表

def count_letter(sentence, accents, case): 

    lower_case_e = ['e'] 
    upper_case_E = ['E'] 
    accent_lower_case = ['é', 'ê', 'è'] 
    accent_upper_case = ['É', 'Ê', 'È'] 

    for character in sentence:#If statement for optional argument where ignore_accents == True and ignore_case == False. 
     #This loop will count lower case and upper case e as differente letters but will treat accented characters the same. 

     if accents == True and case == False: 
      lower_case_count = sentence.count(lower_case_e) 
      accent_lower_case_count = sentence.count(accent_lower_case) 
      upper_case_count = sentence.count(upper_case_E) 
      accent_upper_case_count = sentence.count(accent_upper_case) 

      total_e_count = lower_case_count + accent_lower_case_count 
      total_E_count = upper_case_count + accent_upper_case_count 

      return {'Total number of lower case e in sentence ignoring accents':total_e_count, 'Total number of upper case E in sentence ignoring accents':total_E_count } 

此功能的点来算字母E,并根据它是否是大写或小写,或者如果它有口音,以字母加在一起。我创建一个文本文件名为sentence.txt,它看起来像这样:

Testing if function can count letter e or E. 

我曾尝试使用下面的代码读取文件:

# Reading the data from sentence.txt as a string 
with open('sentence.txt', 'r') as Sentence_file: 
    Sentence_string=Sentence_file.read().replace('\n', '') 

和读取文件后,我试图调用该函数以下列方式:

count_letter(sentence, True, False) 

然而,当我尝试运行此我得到以下错误:

TypeError: must be str, not list 

任何人有任何想法可能会出错?错误可能是我正在阅读我的txt.file的方式吗?任何建议将不胜感激!

完全错误跟踪看起来是这样的:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-2-b171590ddd67> in <module>() 
    29 with open('Test_Sentence1.txt', 'r') as Sentence1_file: 
    30  Sentence1=Sentence1_file.read().replace('\n', '') 
---> 31 count_letter_e(Sentence1, True, False) 
    32 

<ipython-input-2-b171590ddd67> in count_letter_e(sentence, accents, case) 
    18   if accents == True and case == False:#If statement for optional argument where ignore_accents == True and ignore_case == False. 
    19    #This loop will count lower case and upper case e as differente letters but will treat accented characters the same. 
---> 20    lower_case_count = sentence.count(lower_case_e)#counting lower case e with no accent from the sentence 
    21    accent_lower_case_count = sentence.count(accent_lower_case)#counting lower case e with accents from the sentence 
    22    upper_case_count = sentence.count(upper_case_E)#counting upper case E with no accent from the sentence 

TypeError: must be str, not list 
+2

跟踪有什么确切的错误? – Carcigenicate

+0

这是一个你正在寻找帮助的作业问题吗? – pcurry

+1

该功能肯定是错误的。要么你无用地遍历你的'for'循环多少次,因为你的句子中有字符,最终返回'None',否则你只会执行'for'循环的一次迭代。 –

count()”函数只接受一个字符串作为输入。例如:

lower_case_count = 0 
for lower_case_e_char in lower_case_e: 
    lower_case_count += sentence.count(lower_case_e_char) 
print(lower_case_count) 

sentence.count()期待string,而不是一个列表。将小写,大写等改为字符串。例如,lower_case_e = 'e'而不是['e']。它应该可以解决你的错误。其余的,你需要以许多其他方式改善你的功能。

你可以尝试这样的:

lower_case_e = 'e' 
lower_case_count = sentence.count(lower_case_e) 
accent_lower_case_count = [sentence.count(item) for item in accent_lower_case] 
total_e_count = lower_case_count + sum(accent_lower_case_count) 

您可以从错误消息,看到这一点。看第31行,然后是20,你可以看到,sentence.count()有问题。然后检查错误消息,告诉你出了什么问题,因为函数在期待一个字符串时看到了一个列表。 这些指针应该可以帮助你。

在栈跟踪指示的线,您所呼叫的.count()方法并传递在可变lower_case_e其具有['e']的值。 .count()方法需要一个字符串进行计数,而不是多个值。例如:

>>> testing = 'aba' 
>>> testing.count('a') 
2 
>>> testing.count(['a']) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: expected a string or other character buffer object 

因此,对于价值要统计有多个字符(如重音小写),你将需要通过循环和加起来count()值每串的方法,而不是整个列表一次。

根据你正在处理的字符串的长度,它可能为你节省大量的时间来穿过字符串ONCE,随时计算你的角色。您可以手动累积该值,也可以使用defaultdict进行累加。然后,您可以在完成计数后按照所需参数分列总计。

from collections import Counter 

def count_e(source, ignore_accents=False, ignore_case=False): 

    counts = Counter(source) 
    results = {} 

    lower_case_e = 'e' 
    upper_case_E = 'E' 
    accent_lower_case = ['é', 'ê', 'è'] 
    accent_upper_case = ['É', 'Ê', 'È'] 
    all_letters_e = [lower_case_e, upper_case_E] + accent_upper_case + accent_lower_case 

    lower_e_count = counts[lower_case_e] 
    upper_E_count = counts[upper_case_E] 

    if ignore_case and ignore_accents: 
     total = sum(counts[x] for x in all_letters_e) 
     key = 'Total number of E-shaped letters in sentence ignoring case and accents' 
     results[key] = total 
    elif ignore_case: 
     key = 'Total number of letter {} in sentence ignoring case' 
     results[key.format('E')] = upper_E_count + lower_e_count 

     for uc, lc in zip(accent_upper_case, accent_lower_case): 
      results[key.format(uc)] = counts[uc] + counts[lc] 
    elif ignore_accents: 
     key = 'Total number of {} case letter {} in sentence ignoring accents' 

     lower_accent_total = sum(counts[x] for x in accent_lower_case) 
     results[key.format('lower', lower_case_e)] = lower_e_count + lower_accent_total 

     upper_accent_total = sum(counts[y] for y in accent_upper_case) 
     results[key.format('upper', upper_case_E)] = upper_E_count + upper_accent_total 
    else: 
     key = 'Total number of letter {} in sentence' 
     for letter in all_letters_e: 
      results[key.format(letter)] = counts[letter] 
    return results