2.5 字典 Dictionary

您所在的位置:网站首页 字典的sorted 2.5 字典 Dictionary

2.5 字典 Dictionary

2023-03-31 06:02| 来源: 网络整理| 查看: 265

///++ 四、字典 Dic 【键-值对】 (var可变,let不可变)

///++ 四、字典 Dic 【键-值对】 (var可变,let不可变) func yeDic() { //--- 0. 均为空Dic: var eDic01 = Dictionary() var eDic02 = [String:Int]() var eDic03:Dictionary = [:] var eDic04 = Dictionary() var eDic05 = [String:Any]() //--- 1. let vDic0:Dictionary = ["Key0":"ValueStr", "Key1":15] let vDic00:[String:Any] = ["Key0":"ValueStr", "Key1":15] // [String:Any] 等同于 Dictionary let vDic1 = ["Key0":"ValueStr", "Key1":15] as [String:Any] //转 let vDic2 = ["Key0":"vGoStr", "Key1":"vFarStr"] //仅一种类型可推导 let eDic3 = ["eKey0": 5, "eKey1":15] print("字典的元素个数: \(eDic3.count)") print("字典是否为空: \(eDic3.isEmpty)") //--- 2. Dic的 增 删 改 查: var eDic2 = [String:Any]() //Dictionary() // 2.1 增、改 : 有此Key则更新,无则增加;(两种方式) eDic2["Key0"] = "Value" eDic2["Key1"] = 15 eDic2["Key0"] = "Gofar" //方式一; let eV42a = eDic2.updateValue("WXL", forKey: "Key0")//方式二, 返回可选类型; // 2.2 删 eDic2["Key0"] = nil //方式一; let eV43a = eDic2.removeValue(forKey: "Key1") //方式二, 返回可选类型; // 2.3 查 //print(eDic5["Key0"] as! String) //Any型转String,vDicM0["Key0"]是Any类型, //--- 3. Dic 遍历: for (eK, eValue) in eDic2{ //直接解包得到Key-Value; print(" Dic 遍历1. 同时遍历Key-Value值:key=\(eK), 值=\(eValue) ") } for eK in eDic2.keys.sorted() { // 按key的排序 遍历; print(" Dic 遍历2. 按Key排序 key=\(eK), 值=\(eDic2[eK]) ") } for eK in eDic2.keys{ print(" Dic 遍历3. Key值: \(eK) ") } for eValue in eDic2.values{ print(" Dic 遍历4. Value值: \(eValue) ") } //--- 4. Dic 合并:(注意类型)【字典不可直接+】 var eDic4a = Dictionary() // 4.1 合并:(注意类型)【字典不可直接+】 //let vDicM2 = vDicM0 + vDicM1 //XXX for (eKey,eValue) in eDic4a{ //将 eDic4a 加到 eDic2 中: eDic2[eKey] = eValue } // 4.2 merge 合并: var eDic7 = ["k1":5, "k2":15] eDic7.merge(["k1":25, "k6":35]) { eOld, _ in eOld } // 当有相同的key时用原来的值; ["k1": 5, "k6": 35, "k2": 15] print("字典 合并1: \(eDic7)") eDic7.merge(["k1":55, "k7":77]) { _, eNew in eNew } //当有相同的key时用新的值; ["k2": 15, "k6": 35, "k7": 77, "k1": 55] print("字典 合并2: \(eDic7)") //--- 5. 保持顺序 KeyValuePairs: (字典本身数无序的,在扩容之前位置数稳定的) var eDic8 = ["k1":"value1", "k2":"value2", "k3":"Tye"] let eV8 = eDic8.firstIndex(where: { // 查找到第一个符合条件的为止; $0.value.hasPrefix("value") }) // 得到的数可选类型; let eV9 = eDic8.firstIndex { (key: String, value: String) in value.hasPrefix("value") }// 得到的数可选类型; firstIndex 查找到第一个符合条件的为止; //print("---***888: \(eV8)") // eV8 与 eV9 是一样的,只是写法不一样; //eDic8["k9"] = "DiTye" //在firstIndex查找完后,加入元素(扩容了),eDic8[i]会报错 [因为eV9对应的位置就变了] if let i = eV9{ //let i = eV8 解包可选类型; print("条件筛选的 \(eDic8[i]) ") //(key: "k1", value: "value1") } // var eDic9 : KeyValuePairs = ["k1":5, "k2":7, "k3":9] print("字典 保持顺序:\(eDic9.first!)")//取第一对键值对:(key: "k1", value: 5) } ///++ Dic 字典 End; 截屏2023-01-24 21.15.41.png 截屏2023-01-24 21.15.43.png 截屏2023-01-24 21.15.48.png 截屏2023-01-24 21.15.54.png 截屏2023-01-24 21.16.01.png 截屏2023-01-24 21.16.04.png 截屏2023-01-24 21.16.17.png 截屏2023-01-24 21.16.22.png 截屏2023-01-24 21.16.24.png 截屏2023-01-24 21.16.31.png 截屏2023-01-24 21.16.34.png

字典Dic的操作

截屏2023-01-24 21.16.37.png 截屏2023-01-24 21.16.42.png 截屏2023-01-24 21.16.48.png 截屏2023-01-24 21.17.05.png 截屏2023-01-24 21.17.12.png 截屏2023-01-24 21.17.22.png 截屏2023-01-24 21.17.30.png 截屏2023-01-24 21.17.33.png 截屏2023-01-24 21.17.38.png 截屏2023-01-24 21.17.47.png 截屏2023-01-24 21.17.53.png 截屏2023-01-24 21.18.03.png 截屏2023-01-24 21.18.10.png 截屏2023-01-24 21.18.15.png 截屏2023-01-24 21.18.26.png 截屏2023-01-24 21.18.38.png //--- Dictionary func yeSwift(){ var airports : Dictionary = ["DUB": "Dublin", "TYO": "Tokyo"] var firstStudent = ["Name": "Peter", "Age": "12"] //var firstStudent = ["Name": "Peter", "Age": 12] var secondStudent : Dictionary = ["Name": "Peter", "Age": 25] secondStudent["Name"] secondStudent["Age"] secondStudent["Name"] = "John" secondStudent["Age"] = 18 secondStudent["Gender"] = "Male" secondStudent.count secondStudent.description secondStudent.isEmpty secondStudent.updateValue("Jerry", forKey: "Name") secondStudent.popFirst() secondStudent.removeValue(forKey: "Gender") secondStudent secondStudent.removeAll() firstStudent.first?.key firstStudent.first?.value firstStudent.reversed() for key in firstStudent.keys { print(">>>>>\(key)") } for value in firstStudent.values { print(">>>>>\(value)") } for (key, value) in firstStudent { print(">>>>>\(key): \(value)") } var dicpts : Dictionary = ["iː":"yi", "i,ɪ":"x", "e":"x", "æ":"x", "ɜː":"x", "ə":"x", "ʌ":"x", "uː":"x", "u,ʊ":"x", "ɔː":"x", "ɔ,ɒ":"x", "ɑː":"x", "ei,eɪ":"x", "ai,aɪ":"x", "ɔi,ɔɪ":"x", "au,aʊ":"x", "əu,əʊ":"x", "iə,ɪə":"x", "eə,ɛə":"x", "uə,ʊə":"x", "p":"x", "t":"x", "k":"x", "b":"x", "d":"x", "ɡ":"x", "f":"x", "s":"x", "ʃ":"x", "θ":"x", "h":"x", "v":"x", "z":"x", "ʒ":"x", "ð":"x", "r":"x", "tʃ":"x", "tr":"x", "ts":"x", "dʒ":"x", "dr":"x", "dz":"x", "m":"x", "n":"x", "ŋ":"x", "l":"x", "j":"x", "w":"x"] let keys = Array(dicpts.keys).sorted() print(keys[0]) } //--- 2. Dictionary func yeSwift(){ let laptops = ["Acer Aspire 5 Slim Laptop": 364.99, "Lenovo Legion 5 Gaming Laptop": 999.99, "HP 15 Laptop": 629.00, "Apple MBP 15-inch Laptop": 1_399.99]; let filteredLaptops = laptops.filter { $0.value > 800.00 } print("filteredLaptops: \(filteredLaptops)") let filteredLaptops2 = laptops.filter { $0.key.hasPrefix("Apple") } print("filteredLaptops: \(filteredLaptops2)") let groupedLaptops = Dictionary(grouping: laptops.keys) { $0.first! } print("groupedLaptops: \(groupedLaptops)") var devices = ["iPhone", "iPad", "iWatch", "iPhone"] var devicesCounts = [String: Int]() for device in devices { devicesCounts[device, default: 0] += 1 } print("devicesCounts: \(devicesCounts)") let scores = ["Math": 89, "Physics": 88, "Geography": 78, "History": 92] let doubleScores = scores.map { $0.value * 2 } let roundedCities = scores.mapValues { score -> String in "Your score is \(score)." } print(doubleScores) print(roundedCities) let scores2 = ["Math": "89", "Physics": "88", "Geography": "none", "History": nil] let compactMapValues = scores2.compactMapValues { $0} print(compactMapValues) let compactMapValues2 = scores2.compactMapValues { Int($0 ?? "") } print(compactMapValues2) }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3