Hello,
I was wondering if there is a way in c# to append two Dictionaries of
the same type:
Dictionary<string,objectdic1 and Dictionary<string,objectdic2 into
one new Dictionary<string,object?
(Is there any kind of AddRange such as exisits in generic list)?
Thank you very much!
*** Sent via Developersdex http://www.developersdex.com *** 4 9909
On May 19, 8:43*am, csharpula csharp <csharp...@yahoo.comwrote:
Hello,
I was wondering if there is a way in c# to append two Dictionaries of
the same type:
Dictionary<string,objectdic1 *and Dictionary<string,objectdic2 into
one new Dictionary<string,object?
(Is there any kind of AddRange such as exisits in generic list)?
It's not that simple, what happens if both dics share a common key?
You could simply create one such a method though. You could even use a
delegate to defer the decision of what to do with the shared keys
You could simply create one such a method though.
And in C#3, an extension method (perhaps on IDictionary<TKey,TValue>)
would be trivial. But as Ignacio rightly says - you need to decide how
to treat conflicts. Personally I'd probably just let it blow up by
default...
Marc
Could you please explain how can I create a general method for
Dictionary (tkey,tvalue) merge?
*** Sent via Developersdex http://www.developersdex.com ***
Something like below; this specific example uses C# 3, but you can do
similar with C# 2 - you just lose the ability to use
left.AddRange(right) - you have to do SomeClass.AddRange(left,right)
[perhaps renaming the method].
Marc
using System;
using System.Collections.Generic;
using System.Xml.Linq;
static class Program
{
static void Main()
{
// dictionary
var left = new Dictionary<int, string>();
left.Add(1, "Fred");
left.Add(2, "Barney");
var right = new Dictionary<int, string>();
right.Add(3, "Wilma");
right.Add(4, "Betty");
left.AddRange(right);
}
}
public static class DictionaryExt
{
public static void AddRange<TKey, TValue>(
this IDictionary<TKey, TValuedestination,
IDictionary<TKey, TValuevalues)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");
foreach (var pair in values)
{
destination.Add(pair);
}
}
public static void Merge<TKey, TValue>(
this IDictionary<TKey, TValuedestination,
IDictionary<TKey, TValuevalues)
{
if (destination == null) throw new
ArgumentNullException("destination");
if (values == null) throw new ArgumentNullException("values");
foreach (var pair in values)
{
destination[pair.Key] = pair.Value;
}
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Quentin Crain |
last post by:
Hello again All!
(First, I would like to mention I did try to google
for the answer here!)
Say I am populating a dictionary with a list and
appending. I have written it thusly:
d={}
for...
|
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: Roland Rickborn |
last post by:
Hi folks,
I am relatively new to Python. Although I read a lot of howtos,
introductions and wikis, I am still having trouble ;-)
My querstion:
As the subject says, I'd like to feed a...
|
by: Brian Elmegaard |
last post by:
Hi
I have written the following which works, but I would like to write it
less clumsy. I have a dictionary in which I loop through the keys for
a dynamic programming algorithm. If a key is...
|
by: Ben |
last post by:
Hello...
I have set up a dictionary into whose values I am putting a list. I
loop around and around filling my list each time with new values, then
dumping this list into the dictionary. Or so I...
|
by: Tina I |
last post by:
Hi everyone,
I have a small, probably trivial even, problem. I have the following HTML:
I need to make this into a dictionary like this:
dictionary = {"METAR:" : "ENBR 270920Z 00000KT 9999...
|
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= |
last post by:
Hi Pythonistas!
I've got a question about storing tuples in a dictionary. First, a
small test case which creates a list of dictionaries:
import time
list_of_dicts =
keys =
prev_clk =...
|
by: Dan Upton |
last post by:
This might be more information than necessary, but it's the best way I
can think of to describe the question without being too vague.
The task:
I have a list of processes (well, strings to...
|
by: Gary Herron |
last post by:
Dan Upton wrote:
Yes. Create a list of keys, and loop through it:
pids = procs_dict.keys()
for pid in pids:
if procs_dict.poll() != None
# do the counter updates
del procs_dict
Then the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |