Convert a delimited string to a dictionary<string,string> - c#

I have a string in the format
=key1=value1=key2=value2=key3=value3
I need to convert it to a Dictionary<string,string> for the above mentioned key value pairs.
What would be the best way to go about this?
I've tried this:
var input = "key1=value1=key2=value2=key3=value3";
var dict = Regex.Matches(input , #"\s*(.*?)\s*=\s*(.*?)\s*(=|$)")
.OfType<Match>()
.ToDictionary(m => m.Groups[1].Value, m => m.Groups[2].Value);

This can be accomplished with a little Linq:
"=key1=value1=key2=value2=key3=value3"
.Split('=') // Split into an array of strings
.Skip(1) // Skip the first (empty) value
.Select((v, i) => new { v, i }) // Get value and index
.GroupBy(x => x.i / 2) // Group every pair together
.ToDictionary(g => g.First().v, // First item in group is the key
g => g.Last().v) // Last item in group is the value

var dict = new Dictionary<string,string>();
var input = str.Split(new [] { '=' },StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i<input.Length; i+=2)
{
dict.Add(input[i], input[i+1]);
}

Related

How to get the element of array with Max Value?

I'm trying to get the letter of an array that as a max value of a repeated letters on a string.
I have is this:
var AsciiCode = new int[255];
string word= "Hello everybody";
foreach (char c in word)
{
AsciiCode[c]++;
}
MessageBox.Show(string.Format("The max count is:
{0}\nLetter: {1}", AsciiCode.Max(), AsciiCode.ElementAt(//MAX_VALUE_HERE//) ));
A solution with using Linq can be this:
var res =
word.GroupBy(g => g)
.Select(c => new { c.Key, Count = c.Count() })
.OrderByDescending(o => o.Count)
.FirstOrDefault();
C# Demo

Sum values from a list of string and create a dictionary

I want to calculate and sum all values from a sorted list of string like that
List<string> sortedList = new List<string>();
//name, number, weight
sortedList.Add("A,5,1");
sortedList.Add("A,3,2");
sortedList.Add("B,4,1");
sortedList.Add("B,6,2");
sortedList.Sort();
Input: (List l)
Output: (List<Dicionary<string,int>> result):
A ( (5*1)+ ( 3*2) )
B ( (4*1) + (6*2) )
new List of dictionary <string, int>
1 - dic["A" , 11]
2 - dic["B" , 16]
Using linq you can do this.
Dictionary<string, int> r = sortedList.Select(x => x.Split(','))
.GroupBy(x => x[0])
.ToDictionary(x => x.Key, x => x.Sum(y => int.Parse(y[1])*int.Parse(y[2])));
First split all the string by ,. then group them by first element of array. then convert them into dictionary. first element is key. and the value is sum of element2*element3
First you need to split string so you can get multiplication and key.
You can split using Split method. Also, you need to skip first element after splitting.
var multArray = sortedList.Select (x=>int.Parse (x.Split(',')[1])*int.Parse (x.Split(',')[2]));
var keyArray = sortedList.Select (x=>x.Split(',')[0]);
multArray is array with sums, and keyarray is array with keys. Then you need populate dictionary.
for (int i = 0; i < keyArray.Count(); i ++)
{
if (dictionary.ContainsKey (keyArray[i]))
{
dictionary[keyArray[i]] += multArray [i];
}
else
{
dictionary.Add (keyArray[i], multArray [i]);
}
}

split string to Dictionnary<string, int>

I have a string like that : "content;123 contents;456 contentss;789 " etc..
I would like to split this string to get a Dictionary, but I don't know you to make it. I try to split the string but I got a List only.
The content (before semi colon) is always a unique string.
After the semi colon, I always have a number until I found the space.
the number is always an int (no float needs).
Could someone help me please ?
You can use the following LINQ expression:
"content;123 contents;456 contentss;789"
.Split(' ')
.Select(x => x.Split(';'))
.ToDictionary(x => x[0], x => int.Parse(x[1]));
string input = "content1;123 content2;456 content3;789";
var dict = Regex.Matches(input, #"(.+?);(\d+)").Cast<Match>()
.ToDictionary(m => m.Groups[1].Value, m => int.Parse(m.Groups[2].Value));
You can do something like this:
string value = "content;123 contents;456 contentss;789";
Dictionary<string, int> data = new Dictionary<string,int>();
foreach(string line in value.Split(' '))
{
string[] values = line.Split(';');
if (!data.ContainsKey(values[0]))
{
data.Add(values[0], Convert.ToInt32(values[1]));
}
}
var myList = "content1;number1 content2;number2 content3;number3";
var myDictionary = myList.Split(' ').Select(pair => pair.Split(';')).ToDictionary(splitPair => splitPair[0], splitPair => int.Parse(splitPair[1]));
static void Main(string[] args)
{
string content = "content;123 contents;456 contentss;789";
Dictionary<string, int> result = new Dictionary<string, int>();
content.Split(' ').ToList().ForEach(x =>
{
var items = x.Split(';');
result.Add(items[0], int.Parse(items[1]));
});
foreach(var item in result)
{
Console.WriteLine("{0} -> {1}" , item.Key, item.Value);
}
}

C# Get dictionary first keys of duplicated values

I've got the following Dictionary:
Dictionary<int, int> myDict = new Dictionary<int, int>();
myDict.Add(0, 6);
myDict.Add(1, 10);
myDict.Add(2, 6);
myDict.Add(3, 14);
myDict.Add(4, 10);
myDict.Add(5, 10);
I already know how to get all the duplicates values:
var duplicatedValues = myDict.GroupBy(x => x.Value).Where(x => x.Count() > 1);
But what I want instead is the following: A list with all the keys of the duplicated values, but excluding the last duplicated ones. So in my list above the duplicates values are 10 and 6, and what I want is a list of the following keys: 0, 1, 4 (so excluding 2 and 5).
What is the best way to do this?
Any help would be appreciated. Thanks in advance.
EDIT: I did manage to do it with this piece of code by modifying something I found on the internet, but to be honest I find it a bit dumb to first create a string from the keys and then back into ints. I'm kinda new to the Aggregate-command, so any help how to modify the following code would be welcome:
var lookup = allIDs.ToLookup(x => x.Value, x => x.Key).Where(x => x.Count() > 1);
foreach (var item in lookup) {
var keys = item.Aggregate("", (s, v) => s + "," + v);
string[] split = keys.Split(',');
for (int i = 0; i < split.Length - 1; i++) {
if (!split[i].Equals("")) {
Console.WriteLine("removing card nr: " + split[i]);
CurrentField.removeCardFromField(Convert.ToInt32(split[i]));
}
}
}
This should do it:
var firstKeysOfDupeValues = myDict
.GroupBy(x => x.Value)
.SelectMany(x => x.Reverse().Skip(1))
.Select(p => p.Key);
.ToList();
After grouping by value, the last key for each value group is rejected using .Reverse().Skip(1) (this construct serves double duty: it also rejects the single keys of non-duplicated values) and the keys of the remaining key/value pairs are extracted into the result.
You could use
var allButLastDupKeys = myDict.GroupBy(kv => kv.Value)
.Where(g => g.Count() > 1)
.SelectMany(g => g.Take(g.Count() - 1).Select(kv => kv.Key));
string dupKeys = string.Join(",", allButLastDupKeys); // 0,1,4

How to compare 2 list by characters content and its correspondents double values?

I have 2 lists: a string list and a double list with same length and with same index of correspondence. I need to compare all the strings, find the indexes of the list that has the same characters, independent of its order, and delete the highest double value that corresponds to both,
Example:
List<string> str= new List<string>();
str.add("efc");
str.add("abc");
str.add("cde");
str.add("cab");
str.add("fbc");
List<double> vlr= new List<double>();
vlr.add(0.1);
vlr.add(0.5);
vlr.add(0.4);
vlr.add(0.2);
vlr.add(0.3);
and this case, "abc" => (0.5) must be deleted because "cab" has the same characters AND lower correspondent value =>(0.2).
There is a lambda expression for this 2 arrays??
What I've tried:
var distinct = list .Select((str, idx) => new { Str = str, Idx = idx })
.GroupBy(pair => new HashSet<char>(pair.Str), HashSet<char>.CreateSetComparer())
.Select(grp => grp.OrderBy(p => p.Idx).First())
.ToList();
Here's one way to solve it:
// Pair the strings with their correspondence values
var pairs = str.Zip(vlr, (s, d) => new {s, d});
// Group using a sorted string, eliminating differences due to character order
var groups = pairs.GroupBy(x => new string(x.s.ToCharArray().OrderBy(c => c).ToArray()));
// For each group, retain the item with the lowest correspondence value
var filtered = groups.Select(x => x.OrderBy(y => y.d).First().s);
var newDict = str.Zip(vlr, (s, d) => new { s, d })
.GroupBy(x => String.Join("", x.s.OrderBy(y => y)))
.Select(g => g.OrderBy(x => x.d).First())
.ToDictionary(x => x.s, x => x.d);
here is the code:
var group = str.GroupBy(s => string.Join("", s.ToCharArray().OrderBy(c => c)));
var _vlr = group.Select(g => g.Min(s => vlr[str.IndexOf(s)]));
var _str = group.Select(g => g.OrderBy(s => vlr[str.IndexOf(s)]).First());
and the result:

Categories