简体   繁体   English

如何检查字典是否包含列表中的项目

[英]How to check if a dictionary contains an item in a list

How can I check if a dictionary contains an item, that is in the list.如何检查字典是否包含列表中的项目。 I have following dictionary and following list:我有以下字典和以下列表:

public static List<string> DixieStatesList = new List<string>
        {
            "Alabama", "Arkansas", "Floryda", "Georgia", "KarolinaPołudniowa", "KarolinaPółnocna",
            "Kentucky", "Luizjana", "Mississippi", "Missouri", "Oklahoma", "Teksas", "Tennesee", "Wirginia",
            "WirginiaZachodnia"
        };
public static Dictionary<string, float> WallaceNPPFRpopularities = new Dictionary<string, float>()
        {
            {"Alabama", 6}, {"Alaska", 2}, {"Arizona", 1}, {"Arkansas", 4}, {"Connecticut", 2},
            {"DakotaPołudniowa", 5}, {"DakotaPółnocna", 5}, {"Delaware", 4}, {"Floryda", 4},
            {"Georgia",  6}, {"Idaho", 1.5f}, {"Illinois", 2}, {"Indiana", 5}, {"Iowa", 2},
            {"Kalifornia", 4}, {"Kansas", 5}, {"KarolinaPołudniowa", 6}, {"KarolinaPółnocna", 5},
            {"Kentucky", 3}, {"Kolorado", 3}, {"Luizjana", 4}, {"Maine", 6}, {"Maryland", 5},
            {"Massachusets", 2 }, {"Michigan", 5}, {"Minnesota", 1.5f}, {"Mississippi", 6},
            {"Missouri", 2}, {"Montana", 1.5f}, {"Nebraska", 2}, {"Nevada", 4}, {"NewHampshire", 2},
            {"NewJersey", 4}, {"NowyJork", 1.5f}, {"NowyMeksyk", 2}, {"Ohio", 4}, {"Oklahoma", 4},
            {"Oregon", 1}, {"Pensylwania", 1.5f}, {"RhodeIsland", 3}, {"Teksas", 4}, {"Tennesee", 4},
            {"Utah", 1.5f}, {"Vermont", 5}, {"Waszyngton", 1}, {"Wirginia", 6}, {"WirginiaZachodnia", 6},
            {"Wisconsin", 1.5f}, {"Wyoming", 2}
        };

And I want to use 'foreach' or 'if' or both of them to check if this dictionary contains an element from the list and eventually change the value of the element in dictionary, that have the same key as element in the list by 0.25f.我想使用'foreach'或'if'或两者来检查这个字典是否包含列表中的一个元素并最终改变字典中元素的值,它与列表中的元素具有相同的键0.25 F。

I have never touched before c# but you want sth like that.我在 c# 之前从未接触过,但你想要那样的东西。 And i dont thonk that you can change a static dictionary although, if it wasnt static heres a way:而且我不认为您可以更改静态字典,但是,如果它不是静态的,这是一种方法:

foreach(string key in DixieStatesList){

if(WallaceNPPFRpopularities.ContainsKey(key)
    WallaceNPPFRpopularities[key] =+ 0.25;

}

You can use the Enumerable.Intersect method :您可以使用Enumerable.Intersect 方法

const float Increment = .25f;
foreach (string state in WallaceNPPFRpopularities.Keys.Intersect(DixieStatesList))
{
    WallaceNPPFRpopularities[state] += Increment;

    // Incremented "Alabama" popularity to 6.25.
    // Incremented "Arkansas" popularity to 4.25.
    // Incremented "Floryda" popularity to 4.25.
    // Incremented "Georgia" popularity to 6.25.
    // Incremented "KarolinaPołudniowa" popularity to 6.25.
    // Incremented "KarolinaPółnocna" popularity to 5.25.
    // Incremented "Kentucky" popularity to 3.25.
    // Incremented "Luizjana" popularity to 4.25.
    // Incremented "Mississippi" popularity to 6.25.
    // Incremented "Missouri" popularity to 2.25.
    // Incremented "Oklahoma" popularity to 4.25.
    // Incremented "Teksas" popularity to 4.25.
    // Incremented "Tennesee" popularity to 4.25.
    // Incremented "Wirginia" popularity to 6.25.
    // Incremented "WirginiaZachodnia" popularity to 6.25.
    Console.WriteLine(@$"Incremented ""{state}"" popularity to {WallaceNPPFRpopularities[state]}.");
}

You can simply use the ContainsKey on the the Dictionary to test for the presence of the key and if present get the result with the index.您可以简单地使用 Dictionary 上的 ContainsKey 来测试键是否存在,如果存在则使用索引获取结果。

        float result = -1;

        if(WallaceNPPFRpopularities.ContainsKey("Alaska"))
        {
            result = WallaceNPPFRpopularities["Alaska"];
        }

try this尝试这个

    foreach (var key in DixieStatesList)
    {
        if (WallaceNPPFRpopularities.ContainsKey(key))
            WallaceNPPFRpopularities[key] += 0.25f;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM