C# 关联数组与字典

发布于 2024-10-07 23:45:23 字数 657 浏览 3 评论 0 原文

我想构建一个像这样的字典:

Dictionary<String, ArrayList> myDic = new Dictionary<String, ArrayList>();

最后我想要一个像这样的结构:

["blabla"] => array(1,2,3)
["foo"] => array(1,4,6,8) 
.......

要构建这个,我在一个循环中运行,并在每个循环中构建一些字符串,

第一个问题:

每次如何检查该字符串存在 在字典中,如果它不存在,则在字典中打开一个新条目,其中包含数组列表中的一个元素,如果存在,则仅将另一个元素添加到数组列表中

和另一个问题

我如何根据该字典对此字典进行排序数组列表中的元素数量(按降序排列),例如:

["foo"] => array(1,4,6,2,8)     
["bar"] => array(4,6,2,8) 
["bla"] => array(1,2,3)
["blo"] => array(1,2)    
    .......

谢谢!

I want to build an Dictonary like this :

Dictionary<String, ArrayList> myDic = new Dictionary<String, ArrayList>();

in the end i want a structure like :

["blabla"] => array(1,2,3)
["foo"] => array(1,4,6,8) 
.......

to build this i run in a loop and in every loop build some strings ,

first question :

how to check every time if this string exists
in the dictionary , if its not exists open a new entry in dictionary with one element in the array list, if exists only add another element to the array list

and another question:

how can i sort this dictionary according to number of elements in the array list(In descending order) like :

["foo"] => array(1,4,6,2,8)     
["bar"] => array(4,6,2,8) 
["bla"] => array(1,2,3)
["blo"] => array(1,2)    
    .......

thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昵称有卵用 2024-10-14 23:45:23

使用适合工作的正确工具。您想要的数据结构称为“多字典” - 即从键映射到值序列的字典,而不是从键映射到唯一值的字典。

PowerCollections 代码库 包含 MultiDictionary 的实现,可能可以满足您的需求。我会使用它而不是自己编写。

要将字典排序为按序列长度排序的键/序列对序列,我将使用带有“order by”子句的 LINQ 查询。这似乎是最简单的方法。

Use the right tool for the job. The data structure you want is called a "multi-dictionary" - that is a dictionary that maps from a key to a sequence of values, rather than from a key to a unique value.

The PowerCollections codebase contains an implementation of MultiDictionary that probably does what you want. I would use it rather than writing your own.

To sort the dictionary into a sequence of key/sequence pairs ordered by the length of the sequence, I would use a LINQ query with an "order by" clause. That seems like the easiest way to do it.

却一份温柔 2024-10-14 23:45:23

您应该使用数组或 List 而不是 ArrayList。假设您有一个名为 source 的 Dictionary ,这应该可以工作:

var items = source
    .GroupBy(kvp => kvp.Key)
    .Select(grp => new { Key = grp.Key, Items = grp.Select(kvp => kvp.Value).ToArray() })
    .OrderByDescending(i => i.Items.Length);

解释一下,Dictionary 实现 IEnumerable ;> 所以可以被认为是键值对的序列。 Group by 按键对对进行分组,然后 Select 创建一个匿名类型的序列,其中分别包含 Key 和 Items 属性中的键和关联值。然后,该序列按每个对象的 Items 数组中的项目数排序。

如果您想按创建的数组的长度对它们进行排序,则不能使用字典,因为它们没有排序。

Instead of ArrayList you should use an array or List<T>. Assuming you have a Dictionary<string, int> called source this should work:

var items = source
    .GroupBy(kvp => kvp.Key)
    .Select(grp => new { Key = grp.Key, Items = grp.Select(kvp => kvp.Value).ToArray() })
    .OrderByDescending(i => i.Items.Length);

To explain, Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>> so can be considered a sequence of key-value pairs. Group by groups the pairs by key and then Select creates a sequence of an anonymous type which contains the key and associated values in a Key and Items property respectively. This sequence is then ordered by the number of items in the Items array of each object.

If you want to order them by the length of the created array, you can't use a dictionary since they are not ordered.

梦纸 2024-10-14 23:45:23

要检查字典中是否存在某个键并使用该值(如果存在),可以使用 TryGetValue

ArrayList array;
if(!myDic.TryGetValue("blabla", out array))
{
    array = new ArrayList();
    myDic["blabla"] = array;
}
array.Add(42);

To check if a key exists in a dictionary and use the value if it does, you can use TryGetValue:

ArrayList array;
if(!myDic.TryGetValue("blabla", out array))
{
    array = new ArrayList();
    myDic["blabla"] = array;
}
array.Add(42);
樱花落人离去 2024-10-14 23:45:23

像这样的事情会起作用吗:

        if (myDic.ContainsKey(myString))
            myDic[myString].Add(myNumber);
        else
            myDic.Add(myString, new ArrayList(new int[] {myNumber}));

Would something like this work:

        if (myDic.ContainsKey(myString))
            myDic[myString].Add(myNumber);
        else
            myDic.Add(myString, new ArrayList(new int[] {myNumber}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文