Dictionary、List<KeyValuePair<i>>的组合使用
来源:互联网 发布:沪昆高铁质量 知乎 编辑:程序博客网 时间:2024/05/21 18:49
菜菜遇到一个有意思的问题:Dictionary字典没有排序功能,遇到需要特殊排序的还要倒腾成List<KeyValuePair<>>,再在sort()函数中写委托排序;而List<KeyValuePair<>>只能支持读取操作,即只能get读取操作,无法set改变值;所以遇到既需要遍历改变某些值,然后再按照所需排序这样的操作,往往需要Dictionary和List<KeyValuePair<>>一起操作完成,如下所示:
private List<KeyValuePair<int,string>> getdic(List<KeyValuePair<int, string>> list3, List<string> list5,List<string> list6)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
foreach (KeyValuePair<int, string> var in list3)
{
dic.Add(var.Value,2);
}
//处理既往病史
foreach (string key in list5)
{
switch (key)
{
case "高血压":
case "糖尿病":
case "高血脂":
case "肿瘤":
case "脑卒中":
{
if (dic.ContainsKey(key))
{
dic[key]=dic[key]+1; 字典中值可以改变,但是如果是KeyValuePair,其key与value属性都是get类型,所以只能读取,但是不能设定值
}
else
{
dic.Add(key, 3);
}
}
break;
case "冠心病/心绞痛":
{
if (dic.ContainsKey("心"))
{
dic["心"] = dic["心"] + 1;
}
else
{
dic.Add("心", 3);
}
}
break;
case "慢性支气管炎":
{
if (dic.ContainsKey("肺"))
{
dic["肺"] = dic["肺"] + 1;
}
else
{
dic.Add("肺", 3);
}
}
break;
case "肝炎":
{
if (dic.ContainsKey("肝"))
{
dic["肝"] = dic["肝"] + 1;
}
else
{
dic.Add("肝", 3);
}
}
break;
case "慢性胃炎/胃溃疡":
{
if (dic.ContainsKey("胃"))
{
dic["胃"] = dic["胃"] + 1;
}
else
{
dic.Add("胃", 3);
}
}
break;
case "慢性肠炎":
{
if (dic.ContainsKey("小肠"))
{
dic["小肠"] = dic["小肠"] + 1;
}
else
{
dic.Add("小肠", 3);
}
}
break;
default:
break;
}
}
//处理疾病家族史
foreach (string key in list6)
{
switch (key)
{
case "高血压":
case "中风/脑溢血":
case "糖尿病":
case "肿瘤":
{
if (dic.ContainsKey(key))
{
dic[key] = dic[key] + 1;
}
else
{
dic.Add(key, 3);
}
}
break;
case "冠心病/心绞痛":
{
if (dic.ContainsKey("心"))
{
dic["心"] = dic["心"] + 1;
}
else
{
dic.Add("心", 3);
}
}
break;
default:
break;
}
}
//return dic;
foreach (string key in dic.Keys)
{
list.Add(new KeyValuePair<int, string>(dic[key], key));把字典转化成List<KeyValuePair<>>在sort()函数中写委托按照自己想要的方式排序
}
list.Sort(delegate(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2)
{
return s2.Key.CompareTo(s1.Key);
});
return list;
}
private List<KeyValuePair<int,string>> getdic(List<KeyValuePair<int, string>> list3, List<string> list5,List<string> list6)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
foreach (KeyValuePair<int, string> var in list3)
{
dic.Add(var.Value,2);
}
//处理既往病史
foreach (string key in list5)
{
switch (key)
{
case "高血压":
case "糖尿病":
case "高血脂":
case "肿瘤":
case "脑卒中":
{
if (dic.ContainsKey(key))
{
dic[key]=dic[key]+1; 字典中值可以改变,但是如果是KeyValuePair,其key与value属性都是get类型,所以只能读取,但是不能设定值
}
else
{
dic.Add(key, 3);
}
}
break;
case "冠心病/心绞痛":
{
if (dic.ContainsKey("心"))
{
dic["心"] = dic["心"] + 1;
}
else
{
dic.Add("心", 3);
}
}
break;
case "慢性支气管炎":
{
if (dic.ContainsKey("肺"))
{
dic["肺"] = dic["肺"] + 1;
}
else
{
dic.Add("肺", 3);
}
}
break;
case "肝炎":
{
if (dic.ContainsKey("肝"))
{
dic["肝"] = dic["肝"] + 1;
}
else
{
dic.Add("肝", 3);
}
}
break;
case "慢性胃炎/胃溃疡":
{
if (dic.ContainsKey("胃"))
{
dic["胃"] = dic["胃"] + 1;
}
else
{
dic.Add("胃", 3);
}
}
break;
case "慢性肠炎":
{
if (dic.ContainsKey("小肠"))
{
dic["小肠"] = dic["小肠"] + 1;
}
else
{
dic.Add("小肠", 3);
}
}
break;
default:
break;
}
}
//处理疾病家族史
foreach (string key in list6)
{
switch (key)
{
case "高血压":
case "中风/脑溢血":
case "糖尿病":
case "肿瘤":
{
if (dic.ContainsKey(key))
{
dic[key] = dic[key] + 1;
}
else
{
dic.Add(key, 3);
}
}
break;
case "冠心病/心绞痛":
{
if (dic.ContainsKey("心"))
{
dic["心"] = dic["心"] + 1;
}
else
{
dic.Add("心", 3);
}
}
break;
default:
break;
}
}
//return dic;
foreach (string key in dic.Keys)
{
list.Add(new KeyValuePair<int, string>(dic[key], key));把字典转化成List<KeyValuePair<>>在sort()函数中写委托按照自己想要的方式排序
}
list.Sort(delegate(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2)
{
return s2.Key.CompareTo(s1.Key);
});
return list;
}
0 0
- Dictionary、List<KeyValuePair<i>>的组合使用
- [Dotnet][C#]Dictionary的foreach使用KeyValuePair
- C#中Dictionary的foreach使用KeyValuePair
- Unity3d中Dictionary和KeyValuePair的使用
- c#遍历Dictionary使用KeyValuePair
- KeyValuePair 和 Dictionary 的关系
- [C#] Dictionary 如何使用 foreach (KeyValuePair)
- 利用KeyValuePair遍历Dictionary
- KeyValuePair和Dictionary
- Dictionary and KeyValuePair关系
- List Array Dictionary的使用
- C# Dictionary的用法 KeyValuePair<TKey, TValue> 结构
- Dictionary<string, List<string>> 的使用
- Dictionary List类使用
- 如何遍历并删除Dictionary集合内容——学会使用 KeyValuePair
- List<KeyValuePair<int, DateTime> 用法
- List、Dictionary转Dictionary的性能分析
- KeyValuePair
- 【点击模型学习笔记】Inferring clickthrough rates on ads from click behavior on search results_wsdm2011
- MAVEN备忘录
- rt3070 wifi arm+linux移植;wifi移植参考资料
- shell 中的 2>&1
- 面向对象编程
- Dictionary、List<KeyValuePair<i>>的组合使用
- cronExpression表达式解释:
- CSS控制文本自动换行
- 数字通信介绍(3)信道编码
- JavaScript Inheritance: Pseudoclassical vs. Prototypal
- HTML 页面加载 Flash 插件的几种方法
- Eclipse操作技巧
- 8X12 lcd driver
- Android Volley完全解析(一),初识Volley的基本用法