Arrays Swift如何从一个短语中获取一个包含字符数的词典

Arrays Swift如何从一个短语中获取一个包含字符数的词典,arrays,string,swift,dictionary,Arrays,String,Swift,Dictionary,我想从一个包含单词和每个单词的字符数的字符串创建一个字典 var textToShow:String = "Try not to become a man of success, but" //rather try to become a man of value. Albert Einstein" print(charactersCount(textToShow)) func charactersCount(s: String) -> Dictionary<String

我想从一个包含单词和每个单词的字符数的字符串创建一个字典

var textToShow:String = "Try not to become a man of success, but" //rather try to become a man of value. Albert Einstein"

    print(charactersCount(textToShow))

func charactersCount(s: String) -> Dictionary<String, Int> {
    var words = s.componentsSeparatedByString(" ")
    var characterInWordDictionary = Dictionary<String, Int>()

    for word in words {
            characterInWordDictionary[word] = word.characters.count
    }
    return characterInWordDictionary
}
情况还不错,但: -首先,字典的顺序不对 -第二,我还想要字典里的空格

我想回报的是:

["Try": 3, " ": 1, "not": 3, " ": 1, "to": 2, " ": 1, "become": 6, " ": 1, "a": 1, " ": 1, "man": 3, " ": 1, "of": 2, " ": 1, "success,": 8, " ": 1, "but": 3]
如果有人能在这方面提供指导,那将是令人惊讶的


Tks,

我为您编写了一个小函数,可以实现以下功能:

var textToShow:String = "Try not to become a man of success, but" // rather try to become a man of value. Albert Einstein"



func charactersCount(s: String) -> [(String, Int)] {
    var result = [(String, Int)]()

    var word = String(s[s.startIndex.advancedBy(0)])
    var size = 1

    var space = s[s.startIndex.advancedBy(0)] == " "

    for (var i:Int = 1; i < s.characters.count; i++) {
        if (s[s.startIndex.advancedBy(i)] == " ") {
            if (space) {
                size++
                word.append(s[s.startIndex.advancedBy(i)])
            } else {
                result.append((word, size))
                size = 1
                space = true
                word = " "
            }
        } else {
            if (space) {
                result.append((word, size))
                size = 1
                space = false
                word = String(s[s.startIndex.advancedBy(i)])
            } else {
                size++
                word.append(s[s.startIndex.advancedBy(i)])
            }
        }
    }
    result.append((word, size))

    return result
}

print(charactersCount(textToShow))

首先创建一个空的tupleArray。接下来,使用componentsSeparatedByString分解句子,并使用forEach遍历所有元素(单词)以附加该元素($0=word),其字符计数后跟一个元组(“,1)。然后使用popLast删除额外的元组。试着这样做:

let textToShow = "Try not to become a man of success, but"

var tupleArray:[(String, Int)] = []

textToShow.componentsSeparatedByString(" ")
          .forEach{tupleArray += [($0,$0.characters.count),(" ",1)]}
tupleArray.popLast()
print(tupleArray.description)  // "[("Try", 3), (" ", 1), ("not", 3), (" ", 1), ("to", 2), (" ", 1), ("become", 6), (" ", 1), ("a", 1), (" ", 1), ("man", 3), (" ", 1), ("of", 2), (" ", 1), ("success,", 8), (" ", 1), ("but", 3)]\n"

字典没有顺序。您需要一个元组数组字典是非有序集合,因此您要查找的可能是一个元组数组
(String,Int)
,而不是字典。问题是单词之间的空格长度可能不总是1。还有,如果它以空格结尾呢?或者它是从空间开始的。它不能处理所有的情况。@MateHegedus如果这些情况下字符串不正确formatted@MateHegedus你难道不知道如何编排句子格式吗?看看OP Code,如果我们看的是句子,那么它很好用。但是OP没有说我们正在处理法律判决。只是我们有字符串。他所要求的只是保持它的有序,并在它们之间添加空格
["Try": 3, " ": 1, "not": 3, " ": 1, "to": 2, " ": 1, "become": 6, " ": 1, "a": 1, " ": 1, "man": 3, " ": 1, "of": 2, " ": 1, "success,": 8, " ": 1, "but": 3]
let textToShow = "Try not to become a man of success, but"

var tupleArray:[(String, Int)] = []

textToShow.componentsSeparatedByString(" ")
          .forEach{tupleArray += [($0,$0.characters.count),(" ",1)]}
tupleArray.popLast()
print(tupleArray.description)  // "[("Try", 3), (" ", 1), ("not", 3), (" ", 1), ("to", 2), (" ", 1), ("become", 6), (" ", 1), ("a", 1), (" ", 1), ("man", 3), (" ", 1), ("of", 2), (" ", 1), ("success,", 8), (" ", 1), ("but", 3)]\n"