我試圖創建一個函數,它將一個列表作爲參數,並返回第一個字母的類型作爲列表的名稱,以及這些字母的數量,用於例如,如果我的名單看起來是這樣的:計算列表中的名字的第一個字母
let thisList:[String] = ["Roman", "Serial", "Thomas", "Peter", "Pan", "Other", "Peter", "Remy"]
我希望我的函數的輸出看起來像這樣:
["r": 2, "o": 1, "p": 3, "s": 1, "t": 1]
我目前的功能如下:
func listCounter(usernames: [String])->Dictionary<String,Int>{
var countDict : [ String : Int ] = [:]
var list = [String](countDict.keys)
for user in usernames{
let index = advance(user.startIndex, 0)
//Retrieving the first letter of the name and converting it to a string
var letter = "\(user[index])" as String
letter = letter.lowercaseString
//Checking if the list already has the letter in it
if (find(list, letter) == nil){
countDict[letter] = 1
println("Letter not found, not appended")
//If the letter is in the list, add its current value to 1
}else{
let number = Int(countDict[letter]!)
countDict[letter] = number + 1
}
}
return countDict
}
,但由於某種原因,在我的其他語句也不會加字母,只返回:
["r": 1, "o": 1, "p": 1, "s": 1, "t": 1]
,我想不通爲什麼。
如果任何人都能看到爲什麼,或者有更好的解決方案來解決我的問題,我會非常感激。
我不是一個很好的快速閱讀器,但是不應該在if分支中將該字母添加到列表中嗎? list似乎對我無條件保持爲空 – danh 2015-01-21 00:20:29
yeah正如danh所說,list總是空的,並且從不附加到 – chris 2015-01-21 00:32:00
而不是'var list = [String](countDict.keys)'use dict'var firstCount = [String:Int]( )'來計數事件。 – 2015-01-21 00:34:03