将计数值添加到字典值

编程入门 行业动态 更新时间:2024-10-26 09:21:16
本文介绍了将计数值添加到字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一本词典

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!

更多推荐

将计数值添加到字典值

本文发布于:2023-10-31 07:54:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数值   字典

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!