Reputation: 15
public static List<List<string>> Threads = new List<List<string>>();
...
public static void CheckIfResponseContainWords()
{
foreach (var thread in Threads) {
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
}
}
In Threads
in this case i have 50 Lists. In List there are different number of indexes.
After i loop over the Lists in Threads in CheckIfResponseContainWords
some of the Lists are empty.
Now i want to loop over again all the Lists in Threads
and remove all the empty Lists.
List that its count = 0 to remove.
Maybe there is a way to do it also already in the CheckIfResponseContainWords
method after checking if words not exist ?
But the idea is that after i finish making CheckIfResponseContainWords
to remove empty Lists from Threads
.
How can i do it ?
EDIT
public static void CheckIfResponseContainWords()
{
// Here first to remove from all the Lists in Threads fir index ( index 0 ).
foreach (var thread in Threads) {
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
}
}
EDIT
another small problem.
public static void CheckIfResponseContainWords()
{
foreach (var thread in Threads)
{
if (thread.Count > 0)
thread.RemoveAt(0);
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
}
Threads.RemoveAll(list => list.Count == 0);
}
Before doing this:
if (thread.Count > 0)
thread.RemoveAt(0);
I want to remove it and first do this:
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
But if a word from words not exist already in index 0 of any List then remove the whole List. If one or mroe words exist in any List in Thread in index 0 only then keep and remove the words for the whole List indexs.
It should look like something:
public static void CheckIfResponseContainWords() { foreach (var thread in Threads) {
// Check here every List in Threads index 0 if any word from words exist in it only in index 0. If it does then continue to the thread.RemoveAll(line.....and remove all words from the List.
But if in any List in Threads index 0 none of words exist then remove the whole List and move to the next one dont make the thread.RemoveAll(line.... just remove the List. After doing it to all the Lists those who left remove each List index0: if (thread.Count > 0) thread.RemoveAt(0);
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
}
Threads.RemoveAll(list => list.Count == 0);
}
Check index 0 in any List in Threads if one or more words exist in the List move one and check in this List all indexs for words in those no words exist remove them.
If in index 0 none of the words exist remove the whole List and dont make the line part.
I need first to decide if at index 0 of any list one or more words exist continue if not remove the whole List already and continue to the next one.
Upvotes: 0
Views: 1377
Reputation: 109732
If Threads
is a list of lists, then to remove all the empty lists from it, just do this:
Threads.RemoveAll(list => list.Count == 0);
As for your second question: To remove the first element of a list (if it exists), just do this:
if (thread.Count > 0)
thread.RemoveAt(0);
So your loop would become something like:
foreach (var thread in Threads)
{
if (thread.Count > 0)
thread.RemoveAt(0);
thread.RemoveAll(line => line == "" ||
!WordsList.words.Any(w => line.Contains(w)));
}
Upvotes: 2