這是我正在上課的一個練習。我將此發布到留言板,得到的反饋表明我只使用姓名串列中的姓氏。最終,當它運行時,它只回傳 {Marin:1} 而它應該回傳一個包含所有名字作為鍵的字典,值是名字在串列中出現的次數。任何和所有的幫助表示贊賞。
#Write a function called name_counts. name_counts will take
#as input a list of full names. Each name will be two words
#separated by a space, like "David Joyner".
#
#The function should return a dictionary. The keys to the
#dictionary will be the first names from the list, and the
#values should be the number of times that first name
#appeared.
#
#HINT: Use split() to split names into first and last.
#Add your function here!
def name_counts(name_list):
nameDict = {}
for name in name_list:
name = name.split()
new_name = name[0]
for first_name in new_name:
if first_name in nameDict:
nameDict[new_name] =1
else:
nameDict[new_name]=1
return nameDict
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print (although the order of the keys may vary):
#{'Shelba': 5, 'Maren': 1, 'Nicol': 1, 'David': 2, 'Brenton': 2}
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
print(name_counts(name_list))
uj5u.com熱心網友回復:
問題是兩個 for 回圈。
for name in name_list:
name = name.split()
new_name = name[0]
第一個 for 回圈遍歷名稱串列,拆分它們并將第一個名稱分配給 new_name 當 for 回圈完成時,new_name 的值為“Maren”,它是串列中的最后一個名字。第二個 for 回圈在第一個回圈完成之前不會開始,因此它只知道 new_name 的值為“Maren”
.
for first_name in new_name:
if first_name in nameDict:
nameDict[new_name] =1
else:
nameDict[new_name]=1
下一個 for 回圈遍歷名稱“Maren”,它是一個字串,因此一次一個字符執行,因為沒有字符“M”、“a”、“r”、“e”、“n”在字典默認為 else 子句,該子句每次將鍵“Maren”的值設定為 1。這就是你得到 {"Maren": 1} 的原因
洗掉第二個 for 回圈,只需檢查 new_name 是否在第一個 for 回圈下的 nameDict 中。
uj5u.com熱心網友回復:
你是如此接近你有所有正確的想法,但你做的太多了。只要你有名字,你就可以繼續加 1。如果你使用get字典的方法,這很簡單。
def name_counts(name_list):
nameDict = {}
for name in name_list:
# Split the string into a list
name = name.split()
# Get the first name
first_name = name[0]
# If the key doesn't exist, return 0
count = nameDict.get(first_name, 0)
# Add 1 to the count of names
nameDict[first_name] = count 1
return nameDict
uj5u.com熱心網友回復:
我同意你幾乎就在那里。這是我提出的一個解決方案,它保留了您的大部分原始腳本,但主要只是將“new_name”迭代錯誤更改為 list.append。
new_name_list = []
def name_counts(name_list):
nameDict = {}
for name in name_list:
name = name.split()
new_name_list.append(name[0]) # in the original code, 'new_name' was being reset
# with each iteration to that iteration's current value.
# Instead, there is now a list 'new_name_list'
# which is appended with each iteration.
for first_name in new_name_list:
if first_name in nameDict:
nameDict[first_name] =1 # Here the variable 'new_name' is being replaced. Instead of
# carrying the original logic, which would replace it with 'new_name_list',
# it is replaced with the iterable, 'first_name',
# which seems to be the original intent of the given logic.
else:
nameDict[first_name]=1
return nameDict
name_list = ["David Joyner", "David Zuber", "Brenton Joyner", "Brenton Zuber", "Nicol Barthel", "Shelba Barthel", "Shelba Crowley", "Shelba Fernald", "Shelba Odle", "Shelba Fry", "Maren Fry"] print(name_counts(name_list))
uj5u.com熱心網友回復:
這是我最終做的:
def name_counts(name_list):
nameDict = {}
for name in name_list:
name = name.split()
new_name = name[0]
if new_name in nameDict:
nameDict[new_name] = 1
else:
nameDict[new_name] = 1
return nameDict
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363164.html
上一篇:將資料框轉換為嵌套字典
