if from an array like this one
string[] arr={"a", "b"};
Can i modify it in this way?
arr={"a"};
You can do it in the following different ways:
1)
var idx = 1;
arr = arr.Where((s, i) => i != idx).ToArray();
arr = arr.Where(s => s != "b").ToArray();
var filter = {"b"};
arr = arr.Except(​filter);
try this
arr = new string[] { arr[0] };
Related
I want to convert string[][] to List<List<string>>.
eg.
List<List<string>> listOfListReturned = new List<List<string>>();
string[][] twoDArrOfString = new string[2][];
I want to convert twoDArrOfString to listOfListReturned
Please suggest, how to do it?
Regards,
Vivek
Something like the following will also work:
string[][] twoDArrOfString = new string[2][];
var res = twoDArrOfString
.Where(inner => inner != null) // Cope with uninitialised inner arrays.
.Select(inner => inner.ToList()) // Project each inner array to a List<string>
.ToList(); // Materialise the IEnumerable<List<string>> to List<List<string>>
You need to handle nulls if the inner arrays have not been initialised.
If you aren't going to enumerate through all of these, you might want to drop the final ToList and simply work against the IEnumerable<List<string>> to avoid resolving all inner lists if you don't need to (taking advantage of the deferred execution of enumerables).
Any reason in particular why you are doing this?
List<List<string>> listOfListReturned = new List<List<string>>();
string[][] twoDArrOfString = new string[2][];
twoDArrOfString[0] = new[] {"a", "b"};
twoDArrOfString[1] = new[] {"c", "d"};
foreach (var s in twoDArrOfString)
{
listOfListReturned.Add(new List<string>(s));
}
Or
var result = twoDArrOfString.ToList();
var listOfList = result.Select(x => x.ToList()).ToList();
Not tested just imagine it :)
for (int i = 0; i < twoDArrOfString.Length; i++)
{
for (int j = 0; j < twoDArrOfString[i].Length; j++)
{
listOfListReturned[i].Add(twoDArrOfString[j].ToString());
}
listOfListReturned.Add(listOfListReturned[i]);
}
A more linq-ish version might be
List<List<string>> listOfListReturned = new List<List<string>>()
{
new List<string> {"A", "B"},
new List<string> {"C", "D"},
};
string[][] twoDArrOfString = new string[2][]
{
new [] {"A", "B"},
new []{"C", "D"},
};
var actual = twoDArrOfString.Select(arr => new List<string>(arr)).ToList();
for (var idx = 0; idx < listOfListReturned.Count; idx++) {
CollectionAssert.AreEqual(listOfListReturned[idx], actual[idx]);
}
EDIT: Adam's solution above allowing for empty rows is better.
User enters a series of values into textboxes:
Textbox 1: 10,9,8,7
Textbox 2: 1,2,3,4
Id then like to sort these two string and populate a List<string>. Once sorted (already figured out how to do that part), id like to create a jagged array of the inputs like so:
string[][] Arr = new string[2][];
Arr[0] = new string[] { "10", "9", "8", "7" };
Arr[1] = .....
but instead of manually typing in the values, id like to use the List<string> mentioned above.
Is this possible (thus far, my attempts have failed rather miserably)? If not, could someone suggest a possible alternative approach?
Thanks for your time!
EDIT: Based on the answers, I got it working. Sorry again for not making it clear what I meant by sort.
List<string> tempString = new List<string>();
tempString.Add("10,9,8,7");
tempString.Add("1,2,3");
string[][] Arr = new string[2][];
for (int x = 0; x < 2; x++)
{
string[] values = tempString[x].Split(',').ToArray();
Arr[x] = values;
}
Create lists from the strings:
List<string> list1 = new List<string>(textbox1.Text.Split(','));
List<string> list2 = new List<string>(textbox2.Text.Split(','));
Sort the lists:
list1.Sort();
list2.Sort();
Now you can easily create arrays from the lists:
string[][] Arr = new string[2][];
Arr[0] = list1.ToArray();
Arr[1] = list2.ToArray();
If you want to do it in the other order, i.e. first sort then split, it would be:
List<string> list = new List<string>();
list.Add(textbox1.Text);
list.Add(textbox2.Text);
list.Sort();
string[][] Arr = new string[2][];
Arr[0] = list[0].split(',');
Arr[1] = list[1].split(',');
Arr[0] = textBox1.Text.Split(',');
Arr[1] = textBox2.Text.Split(',');
EDIT If you need preprocessing of the lists, you can just do it like so:
var array1 = textbox1.Text.Split(',').OrderBy(x => x).ToArray();
var array2 = textbox2.Text.Split(',').OrderBy(x => x).ToArray();
// extra processing here
string[][] Arr = new string[2][];
Arr[0] = array1;
Arr[1] = array2;
string[][] Arr = new string[]{textBox1.Text, textBox2.Text} //<--or "tempString"
.Select(s => s.Split(','))
.ToArray();
I am using the below code to read data from a text file row by row. I would like to assign each row into an array. I must be able to find the number or rows/arrays and the number of elements on each one of them.
I would also like to do some manipulations on some or all rows and return their values.
I get the number of rows, but is there a way to to loop something like:
*for ( i=1 to number of rows)
do
mean[i]<-row[i]
done
return mean*
var data = System.IO.File.ReadAllText("Data.txt");
var arrays = new List<float[]>();
var lines = data.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var lineArray = new List<float>();
foreach (var s in line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
{
lineArray.Add(Convert.ToSingle(s));
}
arrays.Add(lineArray.ToArray());
}
var numberOfRows = lines.Count();
var numberOfValues = arrays.Sum(s => s.Length);
var arrays = new List<float[]>();
//....your filling the arrays
var averages = arrays.Select(floats => floats.Average()).ToArray(); //float[]
var counts = arrays.Select(floats => floats.Count()).ToArray(); //int[]
Not sure I understood the question. Do you mean something like
foreach (string line in File.ReadAllLines("fileName.txt")
{
...
}
Is it ok for you to use Linq? You might need to add using System.Linq; at the top.
float floatTester = 0;
List<float[]> result = File.ReadLines(#"Data.txt")
.Where(l => !string.IsNullOrWhiteSpace(l))
.Select(l => new {Line = l, Fields = l.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) })
.Select(x => x.Fields
.Where(f => Single.TryParse(f, out floatTester))
.Select(f => floatTester).ToArray())
.ToList();
// now get your totals
int numberOfLinesWithData = result.Count;
int numberOfAllFloats = result.Sum(fa => fa.Length);
Explanation:
File.ReadLines reads the lines of a file (not all at once but straming)
Where returns only elements for which the given predicate is true(f.e. the line must contain more than empty text)
new { creates an anonymous type with the given properties(f.e. the fields separated by comma)
Then i try to parse each field to float
All that can be parsed will be added to an float[] with ToArray()
All together will be added to a List<float[]> with ToList()
Found an efficient way to do this. Thanks for your input everybody!
private void ReadFile()
{
var lines = File.ReadLines("Data.csv");
var numbers = new List<List<double>>();
var separators = new[] { ',', ' ' };
/*System.Threading.Tasks.*/
Parallel.ForEach(lines, line =>
{
var list = new List<double>();
foreach (var s in line.Split(separators, StringSplitOptions.RemoveEmptyEntries))
{
double i;
if (double.TryParse(s, out i))
{
list.Add(i);
}
}
lock (numbers)
{
numbers.Add(list);
}
});
var rowTotal = new double[numbers.Count];
var rowMean = new double[numbers.Count];
var totalInRow = new int[numbers.Count()];
for (var row = 0; row < numbers.Count; row++)
{
var values = numbers[row].ToArray();
rowTotal[row] = values.Sum();
rowMean[row] = rowTotal[row] / values.Length;
totalInRow[row] += values.Length;
}
How do you query a List<string[]> to get the index of the arrays having matches on their sub-arrays and get a return of type System.Collections.Generic.IEnumerable<string[]> ?
EDIT:
I have this:
string[] report = File.ReadAllLines(#".\REPORT.TXT").AsQueryable().Where(s
=> s.StartsWith(".|")).ToArray();
List<string[]> mylist = new List<string[]>();
foreach (string line in report)
{
string[] rows = line.Split('|');
mylist.Add(rows);
}
and I what to get the the mylist indexes where rows[5] == "foo"
For the original question:
list.Where(array => array.Any(item => item == match))
For the updated one:
result = Enumerable.Range(0, list.Count - 1).Where(i => list[i][5] == "foo");
You do actually need to check if the array has at least 6 items as well:
i => list[i].Length > 5 && list[i][5] == "foo"
You mean something like this?
var haystacks = new List<string[]>();
haystacks.Add(new string[] { "abc", "def", "ghi" });
haystacks.Add(new string[] { "abc", "ghi" });
haystacks.Add(new string[] { "def" });
string needle = "def";
var haystacksWithNeedle = haystacks
.Where(haystack => Array.IndexOf(haystack, needle) != -1);
i have an array below
string stringArray = new stringArray[12];
stringArray[0] = "0,1";
stringArray[1] = "1,3";
stringArray[2] = "1,4";
stringArray[3] = "2,1";
stringArray[4] = "2,4";
stringArray[5] = "3,7";
stringArray[6] = "4,3";
stringArray[7] = "4,2";
stringArray[8] = "4,8";
stringArray[9] = "5,5";
stringArray[10] = "5,6";
stringArray[11] = "6,2";
i need to transform like below
List<List<string>> listStringArray = new List<List<string>>();
listStringArray[["1"],["3","4"],["1","4"],["7"],["3","2","8"],["5","6"],["2"]];
how is that possible?
I think what you actually want is probably this:
var indexGroups = x.Select(s => s.Split(',')).GroupBy(s => s[0], s => s[1]);
This will return the elements as a grouped enumeration.
To return a list of lists, which is what you literally asked for, then try:
var lists = x.Select(s => s.Split(',')).GroupBy(s => s[0], s => s[1])
.Select(g => g.ToList()).ToList();
There's no shorthand like that. You'll have to break into a loop and split each array and add to the list.
Non LINQ version (I must admit its much uglier, but you may have no choice)
var index = new Dictionary<string, List<string>>();
foreach (var str in stringArray) {
string[] split = str.Split(',');
List<string> items;
if (!index.TryGetValue(split[0], out items)) {
items = new List<string>();
index[split[0]] = items;
}
items.Add(split[1]);
}
var transformed = new List<List<string>>();
foreach (List<string> list in index.Values) {
transformed.Add(list);
}