-4

I know the code to efficiently overlay one list on another in python, using dictionaries. Here is what i mean:

input:

 dictA = {'c':2, 'e':1}; 
 print(dictA);

output:

{'c': 2, 'e': 1}

input:

dictB = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0}; 
print(dictB);

output:

{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0}

input:

dictB.update(dict(dictA)); 
print(dictB);

output:

{'a': 0, 'b': 0, 'c': 2, 'd': 0, 'e': 1, 'f': 0}

dictA is overlayed on dictB with the 'c' and 'e' now having the values they were assigned in dictA.

Question: What is the equivalent syntax in C# to overlay one list another as above?

  • 2
    The data structure you've mentioned is `Dictionary` in C# – MyBug18 Apr 01 '20 at 02:53
  • Great - thank you both!! – scorpiotomse Apr 01 '20 at 03:05
  • Grrr, just about to post an answer and this gets closed a minute before. Unless I'm misunderstanding the code in the question, `listA` and `listB`, as their names imply, are both _lists_, **not** _dictionaries_, before and after this operation. That `Dictionary<>` would be the most logical choice in C# and that both answers happen to use `Dictionary<>` I don't think necessarily makes this a dictionary question, and, therefore, not a duplicate of that dictionary question. I'm voting to reopen because this is about lists which, as the sample data shows, have a defined ordering. – Lance U. Matthews Apr 01 '20 at 04:06
  • @BACON I'm pretty sure those are python dictionaries in the question. If thats true, then their is no ordering, and they are the same as Dictionaries in C#. I think the OP just forgot to change their variables from `listA` to `dictA`. – RoadRunner Apr 01 '20 at 04:07
  • @RoadRunner The title and body say "list" three times. `listA` seems to be converted to a dictionary ("using dictionaries") with `dict(listA)`. Even if that's not the case, how do we know that the title and "Question:" at the end aren't to be taken literally? It does say that the code presented is the way to "efficiently overlay" lists in Python; if those are, in fact, Python dictionaries that shouldn't presuppose that they have to be dictionaries in C#, too. Also, that's curious that you were able to answer after the question was closed. – Lance U. Matthews Apr 01 '20 at 04:24
  • @BACON Looks like the question has been reopened. Also having `Console.WriteLine` printing python code is incorrect. I would have just left it as `print`. Those are not lists, but dictionaries, and this question needs to address this. It will only cause confusion to future readers. – RoadRunner Apr 01 '20 at 12:02

3 Answers3

3

You can just use foreach.

foreach (var i in listB)
    listA[i.Key] = i.Value;

Note that listA and listB is actually Dictionary type.

MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • While I think this is a viable solution you've reversed his notation. I'm going to vote yours up over the more complete accepted solution because this is simpler and doesn't require hidden magic with extension methods. – Peter Wone Apr 01 '20 at 03:08
0

I'd suggest doing something like this:

void Main()
{
    var listA = new Dictionary<char, int>() { { 'c', 2 }, { 'e', 1 } };
    var listB = new Dictionary<char, int>() { { 'a', 0 }, { 'b', 0 }, { 'c', 0 }, { 'd', 0 }, { 'e', 0 }, { 'f', 0 },  };

    listA.ForEach(x => listB[x.Key] = x.Value);
}

public static class Ex
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var t in source)
            action(t);
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

If you don't mind using LINQ to create a new Dictionary<char, int>, you could use ToDictionary():

var merged = listB
    .ToDictionary(kvp => kvp.Key, 
                  kvp => listA.TryGetValue(kvp.Key, out int value) ? value : kvp.Value);

Which basically attempts to access the key in listA with TryGetValue(), and if it succeeds it sets the value in listA, otherwise keep the listB value.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75