我要删除字符" - "并替换为""。不过这是一本字典,我不知道怎么做?
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)
答案 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可行替代方案。
使用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)