第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從具有常見(jiàn)值的字典中獲取 TKey,其中 TValue 為 List<string>

從具有常見(jiàn)值的字典中獲取 TKey,其中 TValue 為 List<string>

C#
RISEBY 2022-10-23 16:43:25
我有一本看起來(lái)像這樣的字典:Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>(){    {"a" , new List<string> { "Red","Yellow"} },    {"b" , new List<string> { "Blue","Red"} },    {"c" , new List<string> { "Green","Orange"} },    {"d" , new List<string> { "Black","Green"} },};我需要作為字典輸出,dict其中的公共值List<string>應(yīng)該是鍵,值應(yīng)該是鍵列表。例如:Red: [a,b]Green: [c,d]我不知道如何用listin dictionaryas解決這個(gè)問(wèn)題TValue。請(qǐng)解釋我如何處理字典中的列表。
查看完整描述

2 回答

?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以用扁平化您的字典SelectMany并獲得看起來(lái)像的簡(jiǎn)單列表


"a" - "Red"

"a" - "Yellow"

"b" - "Blue"

"b" = "Red"

// and so on

然后按值分組并從這些組中構(gòu)建一個(gè)新字典。試試這個(gè)代碼:


var commonValues = dict.SelectMany(kv => kv.Value.Select(v => new {key = kv.Key, value = v}))

    .GroupBy(x => x.value)

    .Where(g => g.Count() > 1)

    .ToDictionary(g => g.Key, g => g.Select(x => x.key).ToList());


查看完整回答
反對(duì) 回復(fù) 2022-10-23
?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊

很多循環(huán)......循環(huán)遍歷字典,然后循環(huán)遍歷列表中的每個(gè)值。


var result = new Dictionary<string, List<string>>();


// Loop through each key/value pair in the dictionary

foreach (var kvp in dict)

{

    // kvp.Key is the key ("a", "b", etc)

    // kvp.Value is the list of values ("Red", "Yellow", etc)


    // Loop through each of the values

    foreach (var value in kvp.Value)

    {

        // See if our results dictionary already has an entry for this

        // value. If so, grab the corresponding list of keys. If not,

        // create a new list of keys and insert it.

        if (!result.TryGetValue(value, out var list))

        {

            list = new List<string>();

            result.Add(value, list);

        }


        // Add our key to this list of keys

        list.Add(kvp.Key);

    }

}

如果您想通過(guò)包含多個(gè)項(xiàng)目的條目來(lái)過(guò)濾它,那么您可以執(zhí)行以下操作:


result = result.Where(x => x.Value.Count > 1).ToDictionary(x => x.Key, x => x.Value);

或者,您可以避免循環(huán)并改用 Linq:


// Flatten the dictionary into a set of tuples

// e.g. (a, Red), (a, Yellow), (b, Blue), (b, Red), etc

var result = dict.SelectMany(kvp => kvp.Value.Select(color => (key: kvp.Key, color)))

    // Group by the value, taking the color as the elements of the group

    // e.g. (Red, (a, b)), (Yellow, (a)), etc

    .GroupBy(item => item.color, item => item.key)

    // Filter to the ones with more than one item

    .Where(group => group.Count() > 1)

    // Turn it into a dictionary, taking the key of the grouping

    // (Red, Green, etc), as the dictionary key

    .ToDictionary(group => group.Key, group => group.ToList());

您還可以使用 linq 查詢語(yǔ)法,該語(yǔ)法稍長(zhǎng),但避免了SelectMany:


var result =

    (

        from kvp in dict

        from color in kvp.Value

        group kvp.Key by color into grp

        where grp.Count() > 1

        select grp

    ).ToDictionary(grp => grp.Key, grp => grp.ToList());


查看完整回答
反對(duì) 回復(fù) 2022-10-23
  • 2 回答
  • 0 關(guān)注
  • 116 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)