C# 如何基于列表样式键创建字符串值的串联列表?
我有一本包含以下数据的词典:C# 如何基于列表样式键创建字符串值的串联列表?,c#,list,dictionary,key-value,string-concatenation,C#,List,Dictionary,Key Value,String Concatenation,我有一本包含以下数据的词典: Key Value 1 Introduction 1.1 General 1.1.1 Scope 1.2 Expectations 2 Background 2.1 Early Development ... 我想做的是找到一种方法——在C语言中——创建一个新的列表,或将值附加到一个数组中,该数组的值基于列表样式键连接,如下所示: Key Value Concatenation 1
Key Value
1 Introduction
1.1 General
1.1.1 Scope
1.2 Expectations
2 Background
2.1 Early Development
...
我想做的是找到一种方法——在C语言中——创建一个新的列表,或将值附加到一个数组中,该数组的值基于列表样式键连接,如下所示:
Key Value Concatenation
1 Introduction Introduction
1.1 General Introduction - General
1.1.1 Scope Indroduction - General - Scope
1.2 Expectations Introduction - Expectations
2 Background Background
2.1 Early Development Background - Early Development
...
没有为键设置子级别数,但它将始终采用数字格式。有什么想法吗?在字符串中放一个分隔符,可以用它来分隔,通常是分号;。但你可以使用更多
如果您使用分号,请确保将其从字符串中删除。此解决方案可能不是您能想到的最漂亮的解决方案,但如果您知道您的需求真的那么简单,您可能不想过度使用它,而是直接使用这种方法——请注意,大多数代码只是正确呈现-部分的样板文件;实际的算法部分代码不多:
var dic = new Dictionary<string, string>();
dic.Add("1", "Introduction");
dic.Add("1.1", "General");
dic.Add("1.1.1", "Scope");
dic.Add("1.2", "Expectations");
dic.Add("2", "Background");
dic.Add("2.1", "Early Development");
foreach (var kvp in dic)
{
string item = String.Empty;
int length = kvp.Key.Length - 2;
while (length > 0)
{
var parent = dic[kvp.Key.Substring(0, length)];
if (!String.IsNullOrEmpty(item))
item = String.Format("{0} - {1}", parent, item);
else
item = parent;
length -= 2;
}
if (!String.IsNullOrEmpty(item))
item = String.Format("{0} - {1}", item, kvp.Value);
else
item = kvp.Value;
Console.WriteLine(item);
}
如果您只需要输出显示,请使用类似Maate的答案。如果您在收藏中需要此功能,下面是一个开始,但它只会向上提升一个级别:
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("1", "Introduction");
d.Add("1.1", "General");
d.Add("1.1.1", "Scope");
d.Add("1.2", "Expectations");
d.Add("2", "Background");
d.Add("2.1", "Early Development");
var links = from key in d.Keys
from subKey in d.Keys
where subKey.StartsWith(key) && (subKey.Length == key.Length + 2)
select new
{
Key = key,
SubKey = subKey
};
var a = from key in d.Keys
join link in links on key equals link.SubKey into lj
from sublink in lj.DefaultIfEmpty()
select new
{
Key = key,
Value = d[key],
Concatenation = (sublink == null ? string.Empty : d[sublink.Key] + " - ") + d[key]
};
它获取链接,然后使用从字典到链接的左连接来获取连接。就像我说的,它只上升了一级,所以这不是一个完整的解决方案。需要无限递归。这显然需要清理并提高效率,但您只需使用递归方法即可做到这一点:
static string GetConcatenationRecursively(Dictionary<string, string> d, string key)
{
if (key.Length == 1)
{
return d[key];
}
else
{
return string.Format(
"{0} - {1}",
GetConcatenationRecursively(d, key.Substring(0, key.LastIndexOf('.'))),
d[key]);
}
}
这将被称为:
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("1", "Introduction");
d.Add("1.1", "General");
d.Add("1.1.1", "Scope");
d.Add("1.2", "Expectations");
d.Add("2", "Background");
d.Add("2.1", "Early Development");
List<Tuple<string, string, string>> list = new List<Tuple<string, string, string>>();
foreach (string key in d.Keys)
{
list.Add(new Tuple<string, string, string>(key, d[key], GetConcatenationRecursively(d, key)));
}
也需要大量的错误处理;这显然是假设一个格式良好的输入。但是您应该能够从这里获取它。您可能需要将它从平面数据结构转换为基于树的数据结构;一旦它们进入一个基于树的层次视图,这将非常容易。我相信您会假设这些项目将按照它们列出的顺序进行处理。不是这样的。字典集是无序的。您需要将它们放在一个列表中,或者在迭代之前对它们进行排序(可能是按键的字符串长度排序),这应该是可行的。我的猜测是排序将是问题的最小部分:-当然,代码需要重构才能更具表现力;我的主要观点是,如果需求真的这么简单,那么只需几行代码就可以解决它。如果要求更复杂,我认为值得玩弄一些更复杂的东西,比如你在对OP问题的评论中建议的方向:还要注意,只要值是个位数,这就很有效。例如,1.10.1将不起作用。正如你所说,简单的要求。对不起,我过早地将此帖子标记为已回答。上面的方法非常有效,但有时我确实有两位数的键。我应该在最初的问题中提到这一点。我该如何调整这一点来实现这一点?谢谢你。已编辑文章以切换出键。键的长度为-2。LastIndexOf';这应该能奏效。