Home > Net >  How to check if a dictionary contains an item in a list
How to check if a dictionary contains an item in a list

Time:06-06

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.

CodePudding user response:

I have never touched before c# but you want sth like that. 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;

}

CodePudding user response:

You can use the Enumerable.Intersect method:

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]}.");
}

CodePudding user response:

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.

        float result = -1;

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

CodePudding user response:

try this

    foreach (var key in DixieStatesList)
    {
        if (WallaceNPPFRpopularities.ContainsKey(key))
            WallaceNPPFRpopularities[key]  = 0.25f;
    }
  •  Tags:  
  • c#
  • Related