簡體   English   中英

如何讓我的 output 返回 '*' 每個字母在字符串中出現的次數?

[英]How can I make my output return '*' for the number of times each letter appears in a string?

因此,盡管之前尋求幫助,但很多回復都非常有幫助,但我,作為一個初學者,我很難看到它們是如何被使用的。

盡管如此,我自己又擁有了一個 go ,所以我正在嘗試創建一個程序,該程序將返回 '*' 乘以每個字母在字符串中出現的次數,這就是我到目前為止所擁有的......

def numberofletters(filename: str):
    g = list(filename)
    f = []
    for x in set(g):
        f.append(x)
    return f
def numberofwords(filename):
    r = []
    for x in filename:
        r.append([numberofletters(filename),filename.count(x)*('*')])
    return r
print(numberofwords("How was your day"))

但是,它根本不起作用,因為這是我得到的 output..

[[[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2]]

但是我的代碼應該打印出來的答案是這樣的

請注意,下面的 output 是 output 應該看起來像 btw 的示例

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

請嘗試使用盡可能少的內置函數,也請不要使用 import 和 key = lamda 或類似的東西,因為我有點新,我真的不知道如何使用它

如果您只是修改了我的代碼而不是創建一個新的 ig,那將真的很有幫助,謝謝!

您的字母數numberofletters

  • 不返回字母的數量,而是給定單詞的唯一字母,所以你應該改變你的實現來做到這一點。

  • 在您的示例中沒有收到文件名,而是一個短語或一個字符串,因此您應該將filename參數重命名為更通用的名稱。

以下實現使用字典:

def letter_count(phrase: str) -> dict:
    # Creates a dictionary to store each letter to its count
    counter = {}

    # Iterate over each letter inside your phrase
    for letter in phrase:
        # If this letter is present in the counter,
        # return its count. Otherwise, return zero
        letter_count = counter.get(letter, 0)
        
        # Put this letter into the counter incrementing its value
        counter[letter] = letter_count + 1
        
    return counter

現在,只需對它進行相應的排序和打印:

# Call the function to calculate its value
counter = letter_count("How was your day")

# Sort the dictionary based on the count of each letter in the descending order
sorted_counter = sorted(counter.items(), key=lambda x: x[1], reverse=True)

# Iterate over each letter and count inside the sorted counter
for letter, count in sorted_counter:
    # Print the letter and the `+` character repeated `count` times
    print(letter, '+' * count)

由於您不知道如何使用key=lambda... ,我建議您看一下key functions

這是盡可能簡單和明確的:

def letter_count(filename: str):
    chars = list(filename.lower()) # ['h', 'o', 'w', ' ', 'w', 'a', 's', ' ', 'y', 'o', 'u', 'r', ' ', 'd', 'a', 'y']
    chars_unique = set(chars) # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', ' ', 'y'}
    chars_unique.remove(' ') # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', 'y'}
    result = []
    for x in chars_unique:
        # creates a list e.g. ['w', '**'] and adds it to the result list
        result.append([x, chars.count(x)*('*')])
    return result

l_count = letter_count("How was your day") # [['s', '*'], ['h', '*'], ['u', '*'], ['w', '**'], ['o', '**'], ['d', '*'], ['a', '**'], ['r', '*'], [' ', '***'], ['y', '**']]

for c in l_count:
    print(c[0], c[1])

返回:

a **
r *
y **
h *
s *
u *
o **
d *
w **

如果您希望條形按降序排列,則必須在打印之前對數據進行排序:

# MODIFIED TO ORDER THE BARS
def letter_count(filename: str):
    chars = list(filename.lower()) # ['h', 'o', 'w', ' ', 'w', 'a', 's', ' ', 'y', 'o', 'u', 'r', ' ', 'd', 'a', 'y']
    chars_unique = set(chars) # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', ' ', 'y'}
    chars_unique.remove(' ') # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', 'y'}
    result = []
    for x in chars_unique:
        # creates a list e.g. ['w', '**'] and adds it to the result list
        result.append([x, chars.count(x)*('*')])
    result.sort(key=lambda x: len(x[1]), reverse=True)
    return result

讓我們分解一下這個修改:

.sort()就地對 Python 列表進行排序(直接修改結果)

lambda x: len(x[1])匿名 function,獲取輸入的第一個索引的長度 和下面完全一樣,除了我們可以給下面一個名字f

def f(x):
    return len(x[1])

key=lambda x: len(x[1])定義我們想要排序的對象,在這種情況下是星的數量,它是內部列表的長度第一個索引

result.sort(key=lambda x: x[1], reverse=True)使其降序

這使:

y **
a **
o **
w **
r *
h *
d *
s *
u *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM