我有一個列表字符串: [「a1」,「b0」,「c0」,「a2」,「c1」,「d3」,「a3 「。 我想根據它們的後綴得到一個列表[「a3」,「d3」,「c1」,「b0」]。 例如:「a1」,「a2」,「a3」。結果是「a3」。 這個問題可能很簡單,但我解決不了。 感謝您的幫助!C#查找列表字符串元素,它們的後綴大於其他
回答
正在關注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
]
入住這Example
它分配較短的解決方案,然後我將發佈。鑑於問題的簡單性,我只是想使用嵌套for循環,因爲我懷疑他/她能理解LINQ –
這是一個好主意,它對初學者很有幫助。 –
也@binh你應該知道當你有多個數字時,代碼只會識別第一個數字 –
下面是一個另類,它很長,主要是因爲我試圖解釋每一行
// 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();
參見https://dotnetfiddle.net/BmvFEp ,th anks @Hari普拉薩德,我不知道有用於.NET
謝謝你的回答。 我通過你的例子學到了很多東西。 – binh
這將使你在問題中描述的最大的「後綴」的第一個實例小提琴:
string[] test = { "a3", "d3", "c1", "b0" };
string testResult = test.FirstOrDefault(s => s.Last<char>() == s.Max(t => s.Last<char>()));
在這種情況下,結果是「a3」
- 1. 查找其他字符串的前綴字符串
- 2. 查找其他字符串
- 3. C#在字符串列表中查找元素
- 4. 「Set」查找後綴字符串
- 5. 的Python:將字符串分割在其他列表元素
- 6. 定字符串的列表中找到其他列表
- 7. 字符串列表不同後綴
- 8. 如何在列表中查找字符串元素的模式?
- 9. python在字典的其他列表中查找字典的元素
- 10. C#一個比較字符串列表與其他列表字符串
- 11. 如何查找數組中大於其後所有元素的元素數量?
- 12. 查找不在其他列表中的元素
- 13. 查找元素大於x
- 14. Python:從列表中刪除元素作爲前綴的其他
- 15. 如何搜索多個字符串的字符串,然後用其他字符串替換它們? (Python)
- 16. 連接兩個字符串,以便他們共同的後綴,前綴合並
- 17. 轉換列表以字符串的前綴和後綴
- 18. bash:在多個其他字符串中查找字符串
- 19. 查找字符串是否與其他字符串連接
- 20. django模板,查找字符串替換爲其他字符串
- 21. 如何使用grep匹配字符串,然後查找其他字符串
- 22. 查找大字符串的子序列
- 23. 查找列表中的最大元素及其索引 - Prolog
- 24. 找到與他們之間的事情其他兩個字符串後的字符串
- 25. 在C中查找字符串中的最短前綴C
- 26. 用於查找列表最大元素的java流的性能
- 27. 基於其他列的Sharepoint列查找
- 28. 如何檢測字符串後綴並從列表中刪除這些後綴元素? - Python
- 29. 關於查找字符串的中間元素
- 30. 檢查元組列表中的字符串,然後訪問它打印它
因爲我沒有正確的方法來解決這個問題,所以沒有代碼提供。我的問題是明確的嗎?你能告訴我你的方式嗎? – binh
你的問題不清楚,不。爲什麼a3,d3,c1和b0特別?你沒有給我們製作輸出的規則。 – Rob
對不起,我的錯誤,[「a3」,「d3」,「c1」,「b0」] – binh