集合-字典(Lookup/SortedDictionary)
Lookup<TKey, TElement>非常类似于Dictionary<TKey, TValue>,但是把键映射在一个值集上。
必须调用ToLookup方法创建Lookup<TKey, TElement>对象。Lookup<TKey, TElement>对象是不可变的,无法向对象添加,移除元素。
public class User
{
public string Address;
public string Name;
public int Age;
}
class Program
{
static void Main(string[] args)
{
List<User> users = new List<User> { new User{Address="bj",Name="rxm",Age=},
new User{Address="bj",Name="cwr",Age=},
new User{Address="sh",Name="zcl",Age=},
new User{Address="zz",Name="hst",Age=}}; Lookup<char, string> needUsers = (Lookup<char, string>)users.ToLookup(p => Convert.ToChar(p.Address.Substring(, )),
p => p.Name + "-" + p.Age.ToString()); foreach (IGrouping<char, string> item in needUsers)
{
Console.WriteLine(item.Key); //b s z
foreach (string str in item)
{
Console.WriteLine(" {0}", str);
}
}
//-----
Console.WriteLine("--------------");
IEnumerable<string> results = needUsers['b'];
foreach (var item in results)
{
Console.WriteLine(item);
}
Console.Read();
}
}
SortedDictionary<Tkey,Tvalue>中的键都必须唯一。与这个Dictionary<Tkey,Tvalue>类不同的是,它排了序。
SortedDictionary<string, string> items = new SortedDictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);//忽略大小写
items.Add("masanli", "逗你玩");
items.Add("maji", "宇宙牌香烟");
items.Add("liubaorui", "连升三级");
items.Add("houbaolin", "北京话");
foreach (KeyValuePair<string, string> item in items)
{
Console.WriteLine(string.Format("key:{0},value:{1}", item.Key, item.Value));
}
集合-字典(Lookup/SortedDictionary)的更多相关文章
- Python集合&字典
Python集合&字典 @ 目录 字典 字典的几种定义方式 第一种 第二种 第三种 字典的一些基本操作 通过key得到value 增加字典键值对 删除字典键值对 格式化操作 清空字典 复制字典 ...
- 【python基础】之元组 集合 字典
元组 元组:元组和列表类似.但是元组中的元素是固定的(不能给一个元组添加,删除和替换元素以及重新排序) 1.创建元组 t1 = () #创建一个空元组 t2 = (1, 2, 3) t3 = tupl ...
- day11.1函数进阶 列表集合 字典中的函数变量,函数作为形参
函数进阶 1.函数作为变量 a=123 name="gao" nums=[1,2,3] data=nums#指向同一个内存地址 #查看内存地址篇章 def func(): prin ...
- Python入门 [输出,注释,列表,元祖,集合,字典,if,while,for]
print("Hello Python") #输出 ''' 多行注释 用 三个引号 ''' a=1 #赋值 变量首字母只能书字母下划线 第二个字符数字,字母,下划线 声明一个列表 ...
- Python集合字典运算符
1.集合2.字典3.运算符优先级 1.集合 创建:{} set([]) 注意:创建空的集合要用set() 特点:元素唯一,无序 运算: & 交集 | 并集 - 差集 方法: s ...
- python之字符串,列表,集合,字典方法
字典内置函数&方法 函数: 1.len(dict1):打印字典的键的个数 方法:dict1.( ) 2.clear():清空字典 3.copy():复制字典 4.fromkeys():使用指定 ...
- 集合-字典(Dictionary)
字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素.比较像List<T>类,但没有list向后移动元素的性能开销. .net中最主要的字典类是Dict ...
- Dictionary集合 字典
1 Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(,"张三"); ...
- Python 基础补充(一) 列表、元组、集合、字典的区别和相互转换
一.列表.元组.集合.字典的区别 列表 元组 集合 字典 英文 list tuple set dict 可否读写 读写 只读 读写 读写 可否重复 是 是 否 是 存储方式 值 值 键(不能重复) ...
随机推荐
- java fork-join框架应用和分析
http://shmilyaw-hotmail-com.iteye.com/blog/1897636 java fork-join框架应用和分析 博客分类: concurrency multithre ...
- JSON.stringify 应用
原文:https://technet.microsoft.com/zh-cn/sysinternals/cc836459 url JSON.stringify 函数 将 JavaScript 值转换为 ...
- lldp中与snmp相关内容agentx
struct lldpd { int g_snmp; struct event *g_snmp_timeout; void *g_snmp_fds; char *g_snmp_agen ...
- word2vec c代码使用说明
摘要: 1 分词 将文本语料进行分词,以空格,tab隔开都可以.生成分词后的语料 2 训练 对分词后的语料test.txt 进行训练得到模型文件vectors.bin /word2vec -train ...
- LeetCode OJ 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- bower 教程
1.运行 - cmd 2.npm install -g bower 3.文件夹右击git bash here
- nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】
转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...
- linux脚本定期执行
vi /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # .----------- ...
- Inno Setup入门(十五)——Inno Setup类参考(1)
分类: Install Setup 2013-02-02 11:27 536人阅读 评论(0) 收藏 举报 nno setup脚本能够支持许多的类,这些类使得安装程序的功能得到很大的加强,通过对这些类 ...
- PCI源码学习笔记
这个是PCI的关键结构体. static const struct file_operations proc_bus_pci_operations = { .owner = THIS_MODULE, ...