【问题标题】:How to set all found list words values at once in .NET?如何在 .NET 中一次设置所有找到的列表词值?
【发布时间】:2021-11-23 21:30:19
【问题描述】:

我有这样的课程:

    public class cls_words : IEquatable<cls_words>
    {
        public int indx { get; set; }
        public string wordTxt { get; set; }
        public int wordIsFound { get; set; }

        public override string ToString()
        {
            return "ID: " + wordIsFound + "   Name: " + wordTxt;
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            cls_words objAsWord = obj as cls_words;
            if (objAsWord == null) return false;
            else return Equals(objAsWord);
        }
        public override int GetHashCode()
        {
            return wordIsFound;
        }

        public bool Equals(cls_words other)
        {
            if (other == null) return false;
            return (this.wordIsFound.Equals(other.wordIsFound));
        }
    }

基本上类是一个词,无论它是否在搜索中找到。

所以我创建了这个类的列表:

List<cls_words> wordsIn = new List<cls_words>();

wordsIn.Add(new cls_words { indx= 1, wordTxt = "test", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 2, wordTxt = "the", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 3, wordTxt = "test", wordIsFound=0 });

然后,当我搜索列表以查看它是否包含单词时,我想在适当的情况下将所有 wordIsFound 值设置为 1。列表中的某些单词可能相同。

类似

string wordSearch = "test";

if (wordsIn.Exists(x => x.wordTxt == wordSearch)) {

   //set all wordIsFound = 1 where word matches wordSearch 

}

那么我如何在列表中的第一个和第三个项目上将 wordIsFound 设置为 1(与 wordSearch 匹配的项目?

【问题讨论】:

  • 这个逻辑没有意义,如果一个单词有值,为什么要“保存”到变量中而不是检查它?
  • foreach(cls_words cw in wordsIn) cw.wordIsFound = wordsIn.Count(x=&gt; x.wordTxt == cw.wordTxt)-1;foreach(cls_words cw in wordsIn) cw.wordIsFound = wordsIn.Count(x=&gt; x.wordTxt .Equals(cw.wordTxt))-1;

标签: c# .net list match


【解决方案1】:
string wordSearch = "test";
 foreach (cls_words clsword in wordsIn) {
   if(cls_word.wordTxt == wordSearch) {
      clsword.wordIsFound = 1;
   }
}

【讨论】:

  • 那是最好/唯一的方法吗?在循环中?
  • 感谢回复,我认为可能有比循环更优雅的方式。例如,行:``` if (wordsIn.Exists(x => x.wordTxt == wordSearch)) { ``` ....检查所有单词而不是循环。我知道它必须在幕后循环,但我认为可能有类似的设置代码。我猜不是。
  • @E.D.是的。有一个 ForEach 函数,但在内部做同样的事情,但性能和易读性较差:S
【解决方案2】:

更简单的方法可能是将 HashSet 与您的类型一起使用(甚至只是 string)。

HashSet<string> foundWords = new HashSet<string>();
HashSet<string> allWords = new HashSet<string>(); 
allWords.Add("test"); 
allWords.Add("apple");
allWords.Add("test"); // This will just "fail silently" if the word is already in the allWords HashSet.

// then to test:
string wordSearch = "test";
if (allWords.Contains(wordSearch)) foundWords.Add(wordSearch);

// you can even loop over your input of a very big string with something like:
string bigString = "this is a long string with many words";
int maxWords = 1000;
string[] allWordsToTest = bigString.Split(' ', maxWords);
foreach (string s in allWordsToTest)
    if (allWords.Contains(s)) foundWords.Add(s);

显然您会想要进行一些处理(将单词设置为一致的大小写,清空试验之间的“找到的集合”等),但这应该可以帮助您开始。

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2017-01-26
    • 2021-09-05
    相关资源
    最近更新 更多