简体   繁体   English

如何从列表中删除列表?

[英]How can i remove Lists from List?

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. 在这种情况下, Threads中有50个列表。 In List there are different number of indexes. 在列表中有不同数量的索引。 After i loop over the Lists in Threads in CheckIfResponseContainWords some of the Lists are empty. 在我循环CheckIfResponseContainWords中的线程中的列表后,某些列表为空。 Now i want to loop over again all the Lists in Threads and remove all the empty Lists. 现在,我想再次遍历Threads中的所有列表,并删除所有空列表。 List that its count = 0 to remove. 列出其计数= 0删除。

Maybe there is a way to do it also already in the CheckIfResponseContainWords method after checking if words not exist ? 在检查单词是否不存在之后,也许还可以在CheckIfResponseContainWords方法中执行此CheckIfResponseContainWords吗? But the idea is that after i finish making CheckIfResponseContainWords to remove empty Lists from Threads . 但想法是,在我完成CheckIfResponseContainWords以从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. 但是,如果任何列表的索引0中都不存在单词中的单词,则删除整个列表。 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. 如果一个或多个单词仅存在于索引0中的线程的任何列表中,则保留并删除整个列表索引中的单词。

It should look like something: 它看起来应该像这样:

public static void CheckIfResponseContainWords() { foreach (var thread in Threads) { 公共静态无效CheckIfResponseContainWords(){foreach(线程中的var线程){

                // 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); 但是,如果在线程索引0的任何列表中都不存在任何单词,则删除整个列表并移至下一个列表不创建线程.RemoveAll(line ....只需删除列表。对所有列表执行此操作后,左删除每​​个列表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);
        }
  1. 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. 如果列表中存在一个或多个单词,请检查线程中任何列表中的索引0,然后移动一个,并在此列表中检查不存在单词的单词的所有索引,将其删除。

  2. If in index 0 none of the words exist remove the whole List and dont make the line part. 如果在索引0中不存在任何单词,则删除整个列表,而不将其作为行的一部分。

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. 我首先需要确定在任何列表的索引0处是否存在一个或多个单词(如果尚未删除整个列表,则继续)并继续到下一个单词。

If Threads is a list of lists, then to remove all the empty lists from it, just do this: 如果“ Threads是一个列表列表,则要从其中删除所有空列表,只需执行以下操作:

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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM