使用 LINQ 比较两个列表
如果我有 2 个字符串列表:
List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "111", "999");
如何比较列表中每个项目中的每个单独字符?例如:应该比较“0”与“0”,“1”与“1”,“0”与“0”等。看来我可以使用 SelectMany
但我坚持如何去做
编辑:
这些列表在相互比较时应该返回 true (因为星号表示任何字符,我正在验证以确保每个项目长度正好是 3 个字符)
List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "1*1", "***");
If I have 2 lists of strings:
List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "111", "999");
How can I compare each individual character in each item from the lists? Ex: Should compare "0" with "0", "1" with "1", "0" with "0" and so on. It appears that I can use SelectMany
but I am stuck on how to do it
EDIT:
These lists should return true when compared with each other (as asterisk means any character and I am validating to ensure that each item is exactly 3 chars in length)
List<string> firstList = new List<string>("010", "111", "123");
List<string> secondList = new List<string>("010", "1*1", "***");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用通配符更新
结果:
and
和
Updated with wildcards
Results:
and
and
如果您只想比较列表之间的匹配字符序列:
这将导致
true
,即对于以下两个列表 - 它们的字符序列匹配 ("010111123"
对于两者),它们各自的字符串条目不会:编辑以响应注释:
对于通配符匹配,您可以使用
Zip()
并比较每个字符,如果满足则返回 true根据通配符条件进行匹配,然后检查压缩序列中的每个元素都是true
。var isWildCardMatch = firstList.SelectMany(x => x).Zip(secondList.SelectMany( x => x),(a,b)=>
{
if(a==b || a =='' || b == '')
返回真;
return false;
上述方法跨越了字符串条目边界,这会导致错误匹配 - 这里有一个更好的方法:
If you just want to compare for a matching character sequence between your lists:
This would result in
true
, i.e. for the following two lists - their character sequences match ("010111123"
for both), their individual string entries do not:Edit in response to comments:
For a wildcard match you could use
Zip()
and compare each character, return true if they match based on wildcard conditions, then just check that each element in the zipped sequence istrue
.var isWildCardMatch = firstList.SelectMany(x => x).Zip(secondList.SelectMany( x => x),(a,b) =>
{
if(a==b || a =='' || b == '')
return true;
return false;
Above approach crossed string entry boundaries, which would cause false matches - here a better approach:
假设您要将第一个列表中第一个字符串的第一个字符与第二个列表中第一个字符串的第一个字符进行比较,第一个列表中第一个字符串的第二个字符与第二个列表中第一个字符串的第二个字符进行比较第二个列表等。我可以想到两种实现。
我将从以下开始:
这将生成一个整数列表(或数组,或其他),这些整数对应于两个字符串具有相同字符的索引。
第二个是这样的:
那个只返回在相同索引处相等的任何字符。
Assuming you want to compare the first character of the first string in the first list to the first character of the first string of the second list, the second character of the first string in the first list to the second character of the first string in the second list, etc. I can think of two implementations.
The one I would start with:
That would generate a list (or array, or whatever) of ints that correspond to indexes of which both strings had the same character.
The second would be something like:
That one just returns any characters that are equal at the same indexes.