C#查找后缀大于其他元素的列表字符串元素
我有一个列表字符串: [“a1”、“b0”、“c0”、“a2”、“c1”、“d3”、“a3”]。 我想根据它们的后缀得到一个列表[“a3”,“d3”,“c1”,“b0”]。 示例:“a1”、“a2”、“a3”。结果是“a3”。 这个问题可能很简单,但我解决不了。C#查找后缀大于其他元素的列表字符串元素,c#,list,C#,List,我有一个列表字符串: [“a1”、“b0”、“c0”、“a2”、“c1”、“d3”、“a3”]。 我想根据它们的后缀得到一个列表[“a3”,“d3”,“c1”,“b0”]。 示例:“a1”、“a2”、“a3”。结果是“a3”。 这个问题可能很简单,但我解决不了。 谢谢你的帮助 下面的Linq语句满足您的需要 var result= input.Select(x=> new {letter = x[0], number = x[1], item=x}) // Separate letter
谢谢你的帮助 下面的
Linq
语句满足您的需要
var result= input.Select(x=> new {letter = x[0], number = x[1], item=x}) // Separate letter & number.
.GroupBy(x=>x.letter) // Group on letter and take first element (of max number)
.Select(x=> x.OrderByDescending(o=>o.number).First())
.OrderByDescending(x=>x.number) // Order on number.
.Select(x=>x.item) // get the item.
.ToArray();
输出
[
a3
,
d3
,
c1
,
b0
]
检查一下,下面是一个备选方案,它相当长,主要是因为我试图解释每一行
// create list based on your original text
var list = new List<string> { "a1", "b0", "c0", "a2", "c1", "d3", "a3" };
// use a dictionary to hold the prefix and max suffixes
var suffixMaxDictionary = new Dictionary<string, int>();
// loop through the list
for (int i = 0; i < list.Count; i++)
{
// get the prefix using Substring()
var prefix = list[i].Substring(0, 1);
// if the prefix already exist in the dictionary then skip it, it's already been processed
if (suffixMaxDictionary.ContainsKey(prefix))
continue; // continue to the next item
// set the max suffix to 0, so it can be checked against
var suffixMax = 0;
// loop through the whole list again to get the suffixes
for (int j = 0; j < list.Count; j++)
{
// get the current prefix in the second loop of the list
var thisprefix = list[j].Substring(0, 1);
// if the prefixes don't match, then skip it
// e.g. prefix = "a" and thisprefix = "b", then skip it
if (prefix != thisprefix)
continue;
// get the suffix
// warning though, it assumes 2 things:
// 1. that the second character is a number
// 2. there will only ever be numbers 0-9 as the second character
var thisSuffix = Convert.ToInt32(list[j].Substring(1, 1));
// check the current suffix number (thisSuffix) compared the suffixMax value
if (thisSuffix > suffixMax)
{
// if thisSuffix > suffixMax, set suffixMax to thisSuffix
// and it will now become the new max value
suffixMax = thisSuffix;
}
}
// add the prefix and the max suffix to the dictionary
suffixMaxDictionary.Add(prefix, suffixMax);
}
// print the values to the console
Console.WriteLine("original: \t" + string.Join(",", list));
Console.WriteLine("result: \t" + string.Join(",", suffixMaxDictionary));
Console.ReadLine();
//基于原始文本创建列表
var列表=新列表{“a1”、“b0”、“c0”、“a2”、“c1”、“d3”、“a3”};
//使用字典保存前缀和最大后缀
var supfixmaxdictionary=新字典();
//循环浏览列表
for(int i=0;i后缀最大值)
{
//如果thisSuffix>Suffix最大值,则将Suffix最大值设置为thisSuffix
//现在它将成为新的最大值
后缀max=此后缀;
}
}
//将前缀和最大后缀添加到字典中
Add(前缀,后缀max);
}
//将值打印到控制台
Console.WriteLine(“原始:\t”+string.Join(“,”,list));
Console.WriteLine(“结果:\t”+string.Join(“,”,后缀MaxDictionary));
Console.ReadLine();
另请参见,谢谢@Hari Prasad,我不知道.net中有一把小提琴这将为您提供问题中所述的最大“后缀”的第一个实例:
string[] test = { "a3", "d3", "c1", "b0" };
string testResult = test.FirstOrDefault(s => s.Last<char>() == s.Max(t => s.Last<char>()));
string[]test={“a3”、“d3”、“c1”、“b0”};
字符串testResult=test.FirstOrDefault(s=>s.Last()==s.Max(t=>s.Last());
在本例中,结果是“a3”,因为我没有正确的方法来解决这个问题,所以没有代码提供。我的问题很清楚,对吗?你能告诉我你的路吗?你的问题不清楚,不。为什么a3,d3,c1和b0是特别的?您没有给我们生成输出的规则。很抱歉我的错误,[“a3”,“d3”,“c1”,“b0”]这四个词有什么共同点?好的,现在我明白了。是的,分配比我要发布的更短的解决方案非常容易。考虑到问题的简单性,我打算使用嵌套for循环,因为我怀疑他/她是否能理解LINQ这是一个好主意,对初学者会有帮助。另外@binh你应该知道当你有多个数字时,代码只会识别第一个数字。如果他问我,我会发布我的答案,应该很简单,可以根据需要轻松地将字符串和数字分开。我让OP来处理这个案子谢谢你的回答。通过你的例子,我学到了很多东西。