Reputation: 173
I have a nested list and I need his to fill the values returned from the function. Is something like a two-dimensional matrix, where the number of rows is equal to the length of a list, and the number of columns of the length of another list. The question is how to call index to the elements of a nested list?
List<int> wordids;
List<int> hiddenids;
List<List<int>> inputWeights;
foreach (var wordid in wordids)
{
foreach (var hiddeid in hiddenids)
{
inputWeights[wordid][hiddenid] = GetStrength(wordid, hiddenid);
}
}
P.S. Sorry for my bad english.
Upvotes: 2
Views: 1077
Reputation: 109547
You will need add lists to inputWeights as you go:
Using an outer foreach
outer and an inner for
loop:
var inputWeights = new List<List<int>>();
foreach (int wordid in wordids)
{
var currentRow = new List<int>();
for (int i = 0; i < hiddenids.Count; ++i)
currentRow.Add(GetStrength(wordid, hiddenids[i]));
inputWeights.Add(currentRow);
}
Using two foreach
loops (this is my preferred solution, but opinions may differ!):
var inputWeights = new List<List<int>>();
foreach (int wordid in wordids)
{
var currentRow = new List<int>();
foreach (int hiddenid in hiddenids)
currentRow.Add(GetStrength(wordid, hiddenid));
inputWeights.Add(currentRow);
}
Or using Linq instead of the inner loop:
foreach (int wordid in wordids)
{
var currentRow = new List<int>();
currentRow.AddRange(hiddenids.Select(hiddenid => GetStrength(wordid, hiddenid)));
inputWeights.Add(currentRow);
}
Or even using Linq for the entire thing (it's becoming unintelligible now ;):
var inputWeights = wordids.Select(
wordid => new List<int>(hiddenids.Select(hiddenid => GetStrength(wordid, hiddenid)))
).ToList();
And just to be really complete, here's a solution using Linq Query Syntax (and yes, I spent too much time messing around with this, but I couldn't stop once I'd started... ;)
var inputWeights = (from wordid in wordids
select (from hiddenid in hiddenids
select GetStrength(wordid, hiddenid)).ToList()).ToList();
That's all assuming you actually want (#wordids * #hiddenids) in the results! Is that really true?
For example, if you have 10 wordids and 5 hiddenids, the output will have a total of 50 items.
Upvotes: 4
Reputation: 13207
In your particular case you shouldn't use foreach
, but rather a usual for
-loop, like
List<int> wordids;
List<int> hiddenids;
List<List<int>> inputWeights;
wordids = new List<int>();
hiddenids = new List<int>();
inputWeights = new List<List<int>>();
//add values to your List-objects
foreach (int i = 0; i < wordids.Length; i++)
{
foreach (int j = 0; j < hiddenids.Length; j++)
{
inputWeights[i][j] = GetStrength(wordids[i], hiddenids[j]);
}
}
Upvotes: 1
Reputation: 704
List<List<int>> inputWeights=new List<List<int>>();
for (var i = 1; wordids.Count > i; ++i)
{
for (var h = 1; hiddenids.Count > h; ++h)
{
inputWeights[i-1][h-1] = GetStrength(wordids[i-1], hiddenids[h-1]);
}
}
Upvotes: 0
Reputation: 16277
use two dimensional array or jagged array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
a=jaggedArray[0][1]; //a=3;
Upvotes: 1