简体   繁体   English

在 C# 中有没有办法将链表转换为字符串?

[英]In C# is there a way to convert a linked list to a string?

In C# is there a way to convert a linked list to a string?在 C# 中有没有办法将链表转换为字符串?

I have a linked list of sentences and another linked list of words.我有一个链接的句子列表和另一个链接的单词列表。 I want to check the sentences linked list for the words in the words linked list and was thinking a good approach would be to convert the words linked list to a string.我想检查单词链表中单词的句子链表,并认为一个好的方法是将单词链表转换为字符串。

Also considering using a nested while loop.还考虑使用嵌套的 while 循环。

was thinking a good approach would be to convert the words linked list to a string.正在考虑一个好的方法是将单词链表转换为字符串。

Any time you have a list of X and a list of Y, and you want to check whether any of the elements in X are in Y, what you need is probably a hash set (not a list)任何时候你有一个 X 列表和一个 Y 列表,并且你想检查 X 中的任何元素是否在 Y 中,你需要的可能是一个 hash 集合(不是列表)

Hashsets offer fast lookups of fixed values.哈希集提供固定值的快速查找。 Your algorithm should be:你的算法应该是:

  • load the list of searching-for into the set将搜索列表加载到集合中
  • enumerate the list of searching-in, repeatedly asking if the current item is in the set枚举入入列表,反复询问当前项是否在集合中
    var hs = listOfWords.ToHashSet();

    foreach(var sentence in listOfSentences){
      foreach(var word in sentence.Split()){
        if(hs.Contains(word))
        {
          ...
        }
      }
    }

or in a LINQ flavored approach或采用 LINQ 风格的方法

    var hs = listOfWords.ToHashSet();

    var result = listOfSentences.Where(sentence=> 
       sentence.Split().Any(word => 
           hs.Contains(word)
       )
    );

Caution: c# hashing of strings is, be default, case sensitive and every character contributes to string equality.注意:c# 字符串的散列默认情况下区分大小写,并且每个字符都有助于字符串相等。 For a word list of "hello","world","foo","bar" and a list of sentences of: "Hello world,". "Foo bar."对于"hello","world","foo","bar"的单词列表和“Hello world”的句子列表"Hello world,". "Foo bar." "Hello world,". "Foo bar." - these sentences do NOT contain any of the words in the word list. - 这些句子不包含单词列表中的任何单词。 Hello is not equal to hello , world! Hello不等于helloworld! is not equal to world .不等于world Carefully process your sentences so you are comparing apples with apples - eg strip punctuation, and make case equal, for example仔细处理您的句子,以便您将苹果与苹果进行比较 - 例如去除标点符号,并使大小写相等,例如

转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C#

暂无
暂无

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

相关问题 转换列表的最佳方法是什么<string>列出<type>在 C# 中 - What is the best way to convert List <string> to List<type> in C# JavaScript转换C#列表的方法 <string> 值? - JavaScript way to convert C# List<string> value? C# 检查日期时间列表是否包含链接日期并转换为字符串 - C# Check datetime list whether contains linked dates and convert to string 在 C# 中将列表转换为字符串 - Convert a list to a string in C# 将字符串转换为列表 - C# - Convert String to List - c# C#转换列表 <string> 列出 <TimeSpan> - C# Convert List<string> to List<TimeSpan> 在 C# 中转换列表<dynamic>列出<string> - In C# Convert List<dynamic> to List<string> 转换列表 <string> 列表 <int> 在C#中 - Convert List<string> to List<int> in C# 转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C# 寻找一种将字符串列表转换为有效List的简洁方法 <long> 在C#中 - Looking for a clean way to convert a string list to valid List<long> in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM