[英]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:你的算法应该是:
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
不等于hello
, world!
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仔细处理您的句子,以便您将苹果与苹果进行比较 - 例如去除标点符号,并使大小写相等,例如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.