為什麼我的代碼會針對此問題返回錯誤?

喬丹·帕克

所以我寫了這段代碼,它接受一個文件,filename: str並以 '+' 的形式返回字符串中每個字母存在的次數..這是我的代碼

def letterhelper(filename):
    r = list(filename)
    c_r = set(r)
    c_r.remove(' ')
    c_r.remove(',')
    c_r.remove('.')
    c_r.remove('\n')
    f = []
    for x in c_r:
        f.append([-r.count(x), x])
    return f
def charHistogram(data: str):
    r = open(filename)
    q = r.read()
    g = letterhelper(str.lower(q))
    for t in sorted(g):
        print(t[1], (-t[0]) * '+')

和數據是一個單獨的文件,它將被函數打開 letterhelper()

數據可能包含的示例輸入是...

“我的兄弟姐妹給我壓力”

所以,問題是,當data

Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Praesent ac sem lorem. Integer elementum
ultrices purus, sit amet malesuada tortor
pharetra ac. Vestibulum sapien nibh, dapibus
nec bibendum sit amet, sodales id justo.

函數正確返回

e ++++++++++++++++++++++++
t ++++++++++++++++++
s +++++++++++++++++
i ++++++++++++++++
a +++++++++++++++
m ++++++++++++
r ++++++++++++
u ++++++++++++
l +++++++++
n +++++++++
o +++++++++
c +++++++
d +++++++
p +++++++
b +++++
g ++
h ++
j +
v +
None

但如果data=Someday Imma be greater than the rest

輸出是

c_r.remove(',')
KeyError: ','

我應該進行哪些更改,以便我的代碼正確返回直方圖,例如data“Lorem ipsum .....”何時為提供的所有字符串輸入?

伊尚·卡普爾

以下內容將解決該問題,並使您將來可以更輕鬆地添加更多字符以進行刪除。

def letterhelper(filename):
    r = list(filename)
    c_r = set(r)
    chars_to_remove = (' ', ',', '.', '\n')
    for char in chars_to_remove:
        if char in c_r:
            c_r.remove(char)
    f = []
    for x in c_r:
        f.append([-r.count(x), x])
    return f

.

.
.
.
.
.

如果您需要有關代碼的建議。

def letterhelper(filename):  # I GUESS you wanted the parameter here to be 'data' and not 'filename'
    r = list(filename)  # You don't need to convert str to list for using 'count' method
    c_r = set(r)
    chars_to_remove = (' ', ',', '.', '\n')
    for char in chars_to_remove:
        if char in c_r:
            c_r.remove(char)
    f = []  # You can use list comprehentions
    for x in c_r:
        f.append([-r.count(x), x])  # you don't need to negate the count here, you can reverse the sorted list in the function 'charHistogram'
    return f
def charHistogram(data: str):  # I GUESS you wanted the parameter here to be 'filename' and not 'data'
    r = open(filename)  # It is always a good practice to close the file as soon as you are done with it
    q = r.read()
    g = letterhelper(str.lower(q))
    for t in sorted(g):  # sorted(g, reverse=True)
        print(t[1], (-t[0]) * '+')  # t[0] * '+'

以下是我能想到的。

def letterhelper(data: str):
    chars_to_remove = (" ", ",", ".", "\n")

    # f = []
    # for x in set(data):
    #     if x not in chars_to_remove:
    #         f.append((data.count(x), x))

    # The list comprehention in the next line does exactly the same thing as the for loop above

    f = [(data.count(x), x) for x in set(data) if x not in chars_to_remove]

    return f


def charHistogram(filename: str):
    # r = open(filename)
    # q = r.read()
    # r.close()

    # The with statement in the next line does exactly the same thing as the 3 lines above

    with open(filename) as r:
        q = r.read()

    g = letterhelper(str.lower(q))  # q.lower() will also work
    for t in sorted(g, reverse=True):  # this will sort the list in descending order
        print(t[1], t[0] * "+")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

為什麼此代碼不會導致重新定義錯誤?

為什麼我會收到“錯誤:`mutate()` 列 `regression1` 有問題”?

為什麼我的 Python 代碼僅對某些輸入給出錯誤答案?

如果在我的 catch 塊中引發錯誤的代碼被 if 語句包圍,為什麼 Typescript 會引發錯誤?

我的“生成全加器”附近的代碼有什麼問題(錯誤:未定義模塊)?

為什麼即使我的代碼與另一個 API 一起工作,我也會不斷收到語法錯誤:JSON 解析?

為什麼在運行我的 discord.py 代碼時出現錯誤?

為什麼我的 discord.py 代碼中有這個錯誤

為什麼我的 Javascript 代碼返回對像對象?

我在抓取時做錯了什麼。不為我的代碼返回任何值

為什麼這個閉包代碼在不使用捕獲列表時會拋出錯誤?

“類型錯誤:無法將 'list' 對象隱式轉換為 str” 誰能看到什麼可以修復我的代碼?

str 不可調用錯誤。我正在用 python 製作一個狗頭勝過代碼,我想知道是否有人知道我為什麼會收到這個錯誤

為什麼我的代碼輸出錯誤,將英寸轉換為厘米,反之亦然

我收到此代碼的“ReferenceError: invalid assignment left-hand side”錯誤。我看不出有什麼問題

任何人都知道為什麼它會給我錯誤或如何解決這個問題?

當我嘗試在 Invoice 表上添加外鍵時,為什麼會出現此錯誤?

為什麼我的代碼給出錯誤:“sqlite3.OperationalError:沒有這樣的列:”?

為什麼訪問 Typescript 中缺少的 getter 會返回“未定義”而不是導致編譯錯誤?

為什麼我的指針傳遞函數給了我錯誤的輸出?

我的 Haskell 代碼有什麼問題?

類型錯誤:無法讀取未定義的屬性(讀取“長度”)- 想要解釋為什麼代碼會這樣說

為什麼會發生此錯誤:MongoServerError: unknown operator: $slice

為什麼在嘗試安裝 mongoDB 時會出現此錯誤?

為什麼這段代碼輸出錯誤的答案?

代碼拋出錯誤,寫在結束後但為什麼

為什麼這段代碼顯示測試用例錯誤?

為什麼 Next JS 中的異步函數在我的 Typescript 代碼中返回空對象?

我的代碼有什麼問題?(註冊時功能出現 302 錯誤)+ mysql 數據庫錯誤,未保存正確的登錄詳細信息