如何使用值从字典中删除条目

发布于 2024-09-05 04:33:37 字数 353 浏览 2 评论 0原文

我有一个字典集合,如下所示:

mydic.addvalue(key1, val1)
mydic.addvalue(key2, val1)
mydic.addvalue(key3, val1)
mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)

从上面的字典中,我想删除 value ==“val1” 的所有条目,以便结果只有以下条目:

mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)

我的 VB 源代码位于 VS2008 上,目标为 3.5

I have a dictionary collection as bleow:

mydic.addvalue(key1, val1)
mydic.addvalue(key2, val1)
mydic.addvalue(key3, val1)
mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)

From the above dictionary I want to delete all the entries where value == "val1", so that the result would have only following entry:

mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)

My VB source code is on VS2008 and targeted for 3.5

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

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

发布评论

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

评论(5

绝不服输 2024-09-12 04:33:37

您首先需要找到关联值为 val1 的所有键:

var keysToRemove = mydic.Where(kvp => kvp.Value == val1)
                        .Select(kvp => kvp.Key)
                        .ToArray();

然后您可以删除每个键:

foreach (var key in keysToRemove)
{
    mydic.Remove(key);
}

You first need to find all keys for which the associated value is val1:

var keysToRemove = mydic.Where(kvp => kvp.Value == val1)
                        .Select(kvp => kvp.Key)
                        .ToArray();

Then you can remove each of those keys:

foreach (var key in keysToRemove)
{
    mydic.Remove(key);
}
初熏 2024-09-12 04:33:37

基于用户评论的非 LINQ 答案。

private static void RemoveByValue<TKey,TValue>(Dictionary<TKey, TValue> dictionary, TValue someValue)
{
    List<TKey> itemsToRemove = new List<TKey>();

    foreach (var pair in dictionary)
    {
        if (pair.Value.Equals(someValue))
            itemsToRemove.Add(pair.Key);
    }

    foreach (TKey item in itemsToRemove)
    {
        dictionary.Remove(item);
    }
}

用法示例:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(1, "foo");
dictionary.Add(2, "foo");
dictionary.Add(3, "bar");
string someValue = "foo";
RemoveByValue(dictionary, someValue);

与其他答案相同的警告:如果您的值通过引用确定相等,则您需要做额外的工作。这只是一个基础。

A non-LINQ answer based on a comment by the user.

private static void RemoveByValue<TKey,TValue>(Dictionary<TKey, TValue> dictionary, TValue someValue)
{
    List<TKey> itemsToRemove = new List<TKey>();

    foreach (var pair in dictionary)
    {
        if (pair.Value.Equals(someValue))
            itemsToRemove.Add(pair.Key);
    }

    foreach (TKey item in itemsToRemove)
    {
        dictionary.Remove(item);
    }
}

Example usage:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(1, "foo");
dictionary.Add(2, "foo");
dictionary.Add(3, "bar");
string someValue = "foo";
RemoveByValue(dictionary, someValue);

Same caveat as with the other answers: if your value determines equality by reference, you'll need to do extra work. This is just a base.

爱本泡沫多脆弱 2024-09-12 04:33:37

您还可以使用

var x= (from k in mydic
           where k.Value != val1
           select k).ToDictionary(k=>k.key);

x 不会有任何 val1

You can also use

var x= (from k in mydic
           where k.Value != val1
           select k).ToDictionary(k=>k.key);

x will not have any of the val1's

紫轩蝶泪 2024-09-12 04:33:37
foreach(var key in dict.AllKeys.ToArray())
{
    if(...)
        //remove key or something
}
foreach(var key in dict.AllKeys.ToArray())
{
    if(...)
        //remove key or something
}
孤君无依 2024-09-12 04:33:37

答案很旧,但这就是我在不创建另一个字典的情况下做同样的事情的方法。

foreach (KeyValuePair<TKey, TValue> x in MyDic) {

  if (x.Value == "val1")) 
  {  MyDic.Remove(x.Key); } 
}

The answer it's old but this is how I do the same thing without create another Dictionary.

foreach (KeyValuePair<TKey, TValue> x in MyDic) {

  if (x.Value == "val1")) 
  {  MyDic.Remove(x.Key); } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文