将计数值添加到字典值 [英] Add count value to dictionary value

查看:114
本文介绍了将计数值添加到字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本词典

I have a dictionary

Dim Dic as dictionary(integer, integer) 
123, 3
345, 21
22, 0
56, 1



我按值排序Dic


I sorted Dic by values

Dim DicSort As List(Of KeyValuePair(Of integer, Integer)) = Dic.ToList 

DicSort.Sort(Function(x, y) y.Value.CompareTo(x.Value))
22, 0
25, 1
123, 3
345, 21



我想删除Dic值并输入索引号(或运行时Dic的当前计数)

就像这样,

22,0

25,1

123,2

345,3



我尝试过:




I want remove Dic values and put index number (or current count of Dic at runtime)
Like as this,
22, 0
25, 1
123, 2
345, 3

What I have tried:

Dim OrderDic = Dic.ToDictionary(Function(x) x.Key, Function(y)  Dic.Values.Count)



但我有这个结果,

22,4

25,4

123,4

345,4


But i have this result,
22, 4
25, 4
123, 4
345, 4

推荐答案

检查:

Check this:
Dim oDict As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)
oDict.Add(123, 3)
oDict.Add(345, 21)
oDict.Add(22, 0)
oDict.Add(56, 1)


'sort by key and
'replace values with index of sorted element
oDict = oDict _
	.OrderBy(Function(x) x.Key)	_
	.Select(Function(x, i) New KeyValuePair(Of Integer, Integer)(x.Key, i)) _
	.ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value) 





祝你好运!



Good luck!


这篇关于将计数值添加到字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆