Trying to translate some methods written in Python over into C#. The line looks like this:
d[p] = d.setdefault(p, 0) + 1
What exactly does setdefault do? And is there anything similar I can use in a C# dictionary? Or rather, how can I translate that line into C#?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.
From the Python docs:
setdefault(key[, default])
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
There is no direct implementation of this in the .NET framework, but you can define an extension method:
public static V SetDefault<K,V>(this IDictionary<K,V> dict, K key, V @default)
{
V value;
if (!dict.TryGetValue(key, out value))
{
dict.Add(key, @default);
return @default;
}
else
{
return value;
}
}
Usage:
string key;
Dictionary<string, int> dict;
dict[key] = dict.SetDefault(key, 0) + 1;
Edit -- warning: the following works only for this specific case, not for the general case -- see below.
int value = 0;
d.TryGetValue(p, out value);
d[p] = value + 1;
this is equivalent to the following Python snippet (which is better than the one you show):
d[p] = d.get(p, 0) + 1
setdefault
is like get
(fetch if present, else use some other value) plus the side effect of injecting the key / other value pair in the dict if the key wasn't present there; but here this side effect is useless, since you're about to assign d[p]
anyway, so using setdefault
in this case is just goofy (complicates things and slow you down to no good purpose).
In C#, TryGetValue
, as the name suggests, tries to get the value corresponding to the key into its out
parameter, but, if the key's not present, then it
(warning: the following phrase is not correct:)
just leaves said value alone
(edit:) What it actually does if the key's not present is not to "leave the value alone" (it can't, since it's an out
value; see the comments), but rather to set it to the default value for the type -- here, since 0 (the default value) is what we want, we're fine, but this doesn't make TryGetValue
a general-purpose substitute for Python's dict.get
.
TryGetValue
also returns a boolean result, telling you whether it did manage to get the value or not, but you don't need it in this case (just because the default behavior happens to suit us). To build the general equivalent of Python's dict.get
, you need another idiom:
if (!TryGetValue(d, k)) {
k = whatyouwant;
}
Now this idiom is indeed the general-purpose equivalent to Python's k = d.get(k, whatyouwant)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With