删除" - "字典键中的字符

时间:2017-08-26 14:57:22

标签: swift

我要删除字符" - "并替换为""。不过这是一本字典,我不知道怎么做?

var arrays: [String:[String]] = ["2017-08-21":["red"], "2017-08-20":["red"], "2017-08-19":["red"], "2017-08-18":["red"], "2017-08-17":["red"]]
print(arrays)

3 个答案:

答案 0 :(得分:4)

最简单的解决方案是迭代字典中的键值对,并使用新键和旧值创建一个新字典。

var arrays: [String:[String]] = ["2017-08-21":["red"], "2017-08-20":["red"], "2017-08-19":["red"], "2017-08-18":["red"], "2017-08-17":["red"]]
var formattedDictionary = [String:[String]]()
for (key, value) in arrays {
    formattedDictionary[key.replacingOccurrences(of: "-", with: "")] = value
}
print(formattedDictionary)

答案 1 :(得分:3)

Swift 4

随着Swift 4即将来临,我将添加一个Swift 4可行替代方案。

使用Dictionary的新init(uniqueKeysWithValues:)初始化程序,我们可以仅使用原始中的密钥修改键值对替换字典arrays的内容。字典。 E.g:

import Foundation

var arrays = ["2017-08-21": ["red"], 
              "2017-08-20": ["red"], 
              "2017-08-19": ["red"], 
              "2017-08-18": ["red"], 
              "2017-08-17": ["red"]]

arrays = Dictionary(uniqueKeysWithValues: arrays
    .map { ($0.replacingOccurrences(of: "-", with: ""), $1) })

print(arrays)
/* ["20170820": ["red"], 
    "20170817": ["red"], 
    "20170818": ["red"], 
    "20170821": ["red"], 
    "20170819": ["red"]] */

请注意导入Foundation以访问NSString String(通过桥接可用于Swift:s Dictionary

或者,将上面的Dictionary密钥修改逻辑实现为extension Dictionary { mutating func updateKeys(_ transform: (Key) -> Key) { self = Dictionary(uniqueKeysWithValues: self.map { (transform($0), $1) }) } } 的扩展名:

var arrays = ["2017-08-21": ["red"], 
              "2017-08-20": ["red"], 
              "2017-08-19": ["red"], 
              "2017-08-18": ["red"], 
              "2017-08-17": ["red"]]

arrays.updateKeys { $0.replacingOccurrences(of: "-", with: "") }

print(arrays)
/* ["20170820": ["red"], 
    "20170817": ["red"], 
    "20170818": ["red"], 
    "20170821": ["red"], 
    "20170819": ["red"]] */

允许"就地" (逻辑上,至少)通过一些提供的转换修改字典的键:

transform

请注意,将extension Dictionary { mutating func updateKeys(_ transform: (Key) -> Key, uniquingKeysWith combine: (Value, Value) -> Value) { self = Dictionary(map { (transform($0), $1) }, uniquingKeysWith: combine) } } 应用于两个不同的唯一键自然会产生相同的关键结果,因此我们可能希望修改上面的扩展名以将其考虑在内:

(1)

应用例如在以下示例中,(2)String个键是唯一的,但在应用上述示例中使用的transform后,解析为相同的var arrays = ["2017-08-21": ["red"], // (1) "20170821": ["blue"], // (2) "2017-08-20": ["red"], "2017-08-19": ["red"], "2017-08-18": ["red"], "2017-08-17": ["red"]] arrays.updateKeys({$0.replacingOccurrences(of: "-", with: "") }, uniquingKeysWith: { $0 + $1 }) print(arrays) /* ["20170820": ["red"], "20170817": ["red"], "20170818": ["red"], "20170821": ["red", "blue"], "20170819": ["red"]] */

Value

在这里,我们选择连接transform数组,以防import datetime # Define hours that different times of day start start_of_afternoon = 12 start_of_evening = 16 start_of_night = 19 # Get current time now = datetime.datetime.now() if now.hour < start_of_afternoon: # before 12pm print ('morning') elif start_of_afternoon <= now.hour and now.hour < start_of_evening: # after 12pm and before 4pm print ('afternoon') elif start_of_evening <= now.hour and now.hour < start_of_night: # after 4pm and before 7pm print ('evening') elif start_of_night <= now.hour: # after 7pm print ('good night') 将两个(或更多)键解析为同一个新键。

答案 2 :(得分:-1)

这是一个快速解决方案:

let dictionaryWithOldKeys: [String : [String]] = ["2017-08-21":["red"], "2017-08-20":["red"], "2017-08-19":["red"], "2017-08-18":["red"], "2017-08-17":["red"]]

var dictionaryWithNewKeys: [String : [String]] = [:]

for item in dictionaryWithOldKeys {    
    dictionaryWithNewKeys.updateValue(item.value, forKey: item.key.replacingOccurrences(of: "-", with: "."))
}

print(dictionaryWithNewKeys)