【问题标题】:How can i remove Lists from List?如何从列表中删除列表?
【发布时间】:2014-06-18 11:52:48
【问题描述】:
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)));
    }
}

Threads 在这种情况下,我有 50 个列表。在 List 中有不同数量的索引。 在我遍历CheckIfResponseContainWords 中的线程列表之后,一些列表是空的。 现在我想再次循环Threads 中的所有列表并删除所有空列表。 列出要删除的 count = 0。

在检查单词是否不存在之后,CheckIfResponseContainWords 方法中是否也有一种方法可以做到这一点? 但想法是,在我完成 CheckIfResponseContainWords 之后,从 Threads 中删除空列表。

我该怎么做?

编辑

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

编辑

另一个小问题。

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

在此之前:

if (thread.Count > 0)
                        thread.RemoveAt(0);

我想删除它并首先这样做:

thread.RemoveAll(line => line == "" ||
                                !WordsList.words.Any(w => line.Contains(w)));

但是,如果任何 List 的索引 0 中不存在来自 words 的单词,则删除整个 List。 如果仅在索引 0 的线程中的任何列表中存在一个或多个单词,则保留和删除整个列表索引的单词。

它应该看起来像这样:

公共静态无效 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.

但是,如果在线程索引 0 中的任何列表中不存在任何单词,则删除整个列表并移至下一个,不要创建线程。RemoveAll(line.... 只需删除列表。在对所有列出那些离开的删除每个 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);
        }
  1. 如果 List 中存在一个或多个单词,则检查 Threads 中任何 List 中的索引 0 移动一位,并在此 List 中检查所有不存在单词的单词的索引,删除它们。

  2. 如果在索引 0 中不存在任何单词,则删除整个列表并且不要将行部分。

我首先需要确定在任何列表的索引 0 处是否存在一个或多个单词,如果还没有删除整个列表,则继续并继续下一个。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    如果Threads 是一个列表列表,那么要从中删除所有空列表,只需这样做:

    Threads.RemoveAll(list => list.Count == 0);
    

    至于您的第二个问题:要删除列表的第一个元素(如果存在),只需这样做:

    if (thread.Count > 0)
        thread.RemoveAt(0);
    

    所以你的循环会变成这样:

    foreach (var thread in Threads)
    {
        if (thread.Count > 0)
            thread.RemoveAt(0);
    
        thread.RemoveAll(line => line == "" || 
                    !WordsList.words.Any(w => line.Contains(w)));
    }
    

    【讨论】:

    • 马修它正在工作。还有一个子问题,当我删除线程索引中的一个列表中不存在的单词时,我怎样才能使它从线程中的所有列表中自动删除第一个索引 0。无论单词是否存在或不只是从线程索引0中的任何列表。并在单词存在检查之前进行。
    • Matthew 用我在子问题中的意思更新了我的问题。我需要这样做的原因是我已经在另一个类中所有这些索引都为 0。所以我不需要线程中所有列表中的索引 0。
    • @user3747465 好的,让我考虑一下,如果我能解决它,我会更新我的答案以包含它! ...好的更新。
    • 马修我还有一个问题。我应该打开一个新问题来编辑我的问题并解释吗?
    • @user3747465 如果不是强相关,那么是的,您应该提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 2013-06-14
    • 2021-05-24
    • 1970-01-01
    • 2021-03-10
    • 2021-07-19
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多