【发布时间】:2018-06-03 07:34:32
【问题描述】:
我有两个字符串列表,如果当前索引处的字符串在第二个列表中(反之亦然),我想从每个列表中提取索引,字符串不能完全匹配或者可以是另一个列表的简写,例如,考虑这两个列表
List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };
在上面的例子中,我想从aList索引:1,2,3
从bList 索引 0,1,3
aList 中的索引 1,2 显然是完全匹配的字符串,但有趣的部分是 "EquipType" 和 "EquipmentType" 其中 匹配 因为 “EquipType” 是 “EquipmentType”的简写
但 "PartName" 不是 "PartShortName" 的简写,因此不需要索引
这是我的代码
List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };// 1, 2 , 3
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };//0, 1 ,3
List<int> alistIndex = new List<int>();
List<int> blistIndex = new List<int>();
for (int i = 0; i < aList.Count; i++)
{
string a = aList[i];
for (int j = 0; j < bList.Count(); j++)
{
string b = bList[j];
string bigger, smaller;
int biggerCount, smallerCount;
if (a.Length > b.Length)
{
bigger = a; smaller = b;
biggerCount = a.Length ; smallerCount = b.Length ;
}
else
{
bigger = b; smaller = a;
biggerCount = b.Length; smallerCount = a.Length ;
}
int countCheck = 0;
for (int k = 0; k < biggerCount; k++)
{
if (smaller.Length != countCheck)
{
if (bigger[k] == smaller[countCheck])
countCheck++;
}
}
if (countCheck == smaller.Length)
{
alistIndex.Add(i);
blistIndex.Add(j);
res = true;
break;
}
else
res = false;
}
}
alistIndex.ForEach(i => Console.Write(i));
Console.WriteLine(Environment.NewLine);
blistIndex.ForEach(i => Console.Write(i));
Console.ReadKey();
上面的代码工作得很好,看起来和solution很相似
但是如果像这样改变第二个列表的顺序
List<string> bList = new List<string> { "PartCode", "PartShortName", "PartName", "EquipmentType" };
我将得到索引 0、1 和 3(但我想要 0、2 和 3)
我应该检查每对的距离并返回最低的吗?还是我应该用不同的方法工作
谢谢
附言 我还找到了this GitHub,但我不知道它是否对我有用
【问题讨论】:
-
你认为“EquipType”和“EquipmentType”是一样的吗?
-
@L_J 你是什么意思?
-
请仔细检查您的示例和索引,我可以发誓您自相矛盾。还要清楚你的意思,你不能让它在一种情况下工作,而不是另一种情况下的速记......根据@L_J问题。他试图弄清楚你想要什么,因为它似乎并不明确。
-
@Seabizkit 也许速记不是描述我想要的东西的正确词,但也许“缩写”会更合适(如美国和美国,EquipType 和 EquipmentType 也是如此,但 PartName PartShortName 不是)我是吗?现在更清楚了吗?
-
问题实际上是关于如何比较两个字符串。之后,列表的比较是微不足道的。那么,当您认为两个字符串“相同”时,确切的标准是什么?
标签: c# string fuzzy-comparison