Sorting a Hashtable by Values

本文介绍如何在C#中使用Dictionary存储键值对,并将这些键值对转换为列表进行排序。通过示例代码展示了如何定义和填充字典,接着将其内容转化为列表并按值进行排序。

Dictionary < string , int > dict = new Dictionary < string , int >();

 

/*  populate dictionary */

 

List < KeyValuePair < string , int >> summaryList = new List < KeyValuePair < string , int >>();

 

summaryList .AddRange(dict);

summaryList .Sort

   (

      delegate ( KeyValuePair < string , int > kvp1,

                   KeyValuePair < string , int > kvp2)

         {

            return Comparer < int >.Default.Compare(kvp1.Value, kvp2.Value);

         }

    );

Traceback (most recent call last): File "F:\314\Lib\site-packages\pandas\core\arrays\categorical.py", line 460, in __init__ codes, categories = factorize(values, sort=True) File "F:\314\Lib\site-packages\pandas\core\algorithms.py", line 795, in factorize codes, uniques = factorize_array( File "F:\314\Lib\site-packages\pandas\core\algorithms.py", line 595, in factorize_array uniques, codes = table.factorize( File "pandas/_libs/hashtable_class_helper.pxi", line 7293, in pandas._libs.hashtable.PyObjectHashTable.factorize File "pandas/_libs/hashtable_class_helper.pxi", line 7203, in pandas._libs.hashtable.PyObjectHashTable._unique TypeError: unhashable type: 'list' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "F:/shuju/1/12.py", line 300, in <module> 主程序() File "F:/shuju/1/12.py", line 270, in 主程序 df = 识别器.保存结果(结果列表) File "F:/shuju/1/12.py", line 238, in 保存结果 df = df.sort_values(['周数排序', '果实编号排序']) File "F:\314\Lib\site-packages\pandas\core\frame.py", line 7205, in sort_values indexer = lexsort_indexer( File "F:\314\Lib\site-packages\pandas\core\sorting.py", line 351, in lexsort_indexer cat = Categorical(k, ordered=True) File "F:\314\Lib\site-packages\pandas\core\arrays\categorical.py", line 462, in __init__ codes, categories = factorize(values, sort=False) File "F:\314\Lib\site-packages\pandas\core\algorithms.py", line 795, in factorize codes, uniques = factorize_array( File "F:\314\Lib\site-packages\pandas\core\algorithms.py", line 595, in factorize_array uniques, codes = table.factorize( File "pandas/_libs/hashtable_class_helper.pxi", line 7293, in pandas._libs.hashtable.PyObjectHashTable.factorize File "pandas/_libs/hashtable_class_helper.pxi", line 7203, in pandas._libs.hashtable.PyObjectHashTable._unique TypeError: unhashable type: 'list'
11-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值