2017-10-20 70 views
1

我創建了一個函數來計算字母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'循環的一次迭代。 –

回答

2

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) 
0

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()有問題。然後檢查錯誤消息,告訴你出了什麼問題,因爲函數在期待一個字符串時看到了一個列表。 這些指針應該可以幫助你。

1

在棧跟蹤指示的線,您所呼叫的.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()值每串的方法,而不是整個列表一次。

0

根據你正在處理的字符串的長度,它可能爲你節省大量的時間來穿過字符串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 
相關問題