我有两个列表,并希望生成一个结果,其中单词将逻辑连接。例如,“人是活的”,“老鼠是动物”等等。
List<string> nouns = new List<string> { man[1], woman[2], cat[3], house[4], rat[5], prison[6]};
List<string> descriptions = new List<string> { is alive[1][2][3][5], is made of bricks[4][6], is an animal[3][5], is a building[4][6], is female[2][3][5], is a word [1][2][3][4][5][6]};据我所知,我需要首先使用这些列表来填充哈希表?我该怎么做呢?我应该先在列表中添加特殊的索引吗?
我把它看作一个哈希表,其中所有的第一个单词都有像男人(1),女人(2)等的索引,然后所有的描述都只匹配正确的单词,比如"is alive (1),is a word (1)“。“是活的(%2),是女性(%2),是活的(%2)”。我想知道怎么做。
发布于 2015-05-07 18:38:34
如果除了要用来连接列表的索引之外没有逻辑,则可以使用Enumerable.Zip
IEnumerable<string> result = nouns
.Zip(descriptions, (n, d)=> String.Format("{0} {1}", n, d));结果:
"man is alive"
"woman is made of bricks"
"cat is an animal"
"house is a building"
"rat is female"
"prison is a word" 既然您的问题已经改变,并且您已经澄清了一些事情,那么您似乎不需要两个列表,而是需要一个Dictionary<string, List<int>>。因此,该值包含属于名词的字符串的所有索引。
var nounDescriptions = new Dictionary<string, List<int>>
{
{"man", new List<int>{0, 1, 2, 4}},
// .....
};
foreach (var kv in nounDescriptions)
foreach(int index in kv.Value)
Console.WriteLine("{0} {1}", kv.Key, descriptions[index]);发布于 2015-05-07 18:45:51
解析的可能解决方案(如果格式正确的话):
List<string> nouns = new List<string> {
"man[1]", "woman[2]", "cat[3]", "house[4]", "rat[5]", "prison[6]"};
List<string> descriptions = new List<string> {
"is alive[1][2][3][5]",
"is made of bricks[4][6]",
"is an animal[3][5]",
"is a building[4][6]",
"is female[2][3][5]",
"is a word [1][2][3][4][5][6]" };
// parsed dictionary of entry indice
var dict = descriptions
.Select(item => item.Split(new Char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries))
.SelectMany(items => items.Skip(1).Select(item => new {
name = items[0],
id = int.Parse(item) }))
.GroupBy(pair => pair.id, pair => pair.name)
.ToDictionary(item => item.Key, item => item);
var result = nouns
.Select(item => item.Split(new Char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries))
.SelectMany(items => items.Skip(1).Select(item => new {
name = items[0],
id = int.Parse(item)}))
.SelectMany(pair => dict[pair.id].Select(item => pair.name + " " + item));要打印输出:
Console.Write(String.Join(Environment.NewLine, result));结果是
man is alive
man is a word
woman is alive
woman is female
woman is a word
cat is alive
cat is an animal
cat is female
cat is a word
house is made of bricks
house is a building
house is a word
rat is alive
rat is an animal
rat is female
rat is a word
prison is made of bricks
prison is a building
prison is a word 发布于 2015-05-07 18:36:53
代码
Dictionary<string, string> nouns = new Dictionary<string, string>
{
{ "man", "word,human,animal" },
{ "woman", "word,human,animal,female" },
{ "cat", "word,animal" },
{ "house", "word,construction" },
{ "rat", "word,animal" },
{ "prison", "word,construction" },
};
Dictionary<string, string> descriptions = new Dictionary<string, string>
{
{ "alive", "animal" },
{ "is made of bricks", "construction" },
{ "is an animal", "animal" },
{ "is female", "female" },
{ "is a word", "word" },
};
Dictionary<string, List<string>> nounsByTag =
(from e in nouns
select new
{
Word = e.Key,
Tags = (from t in e.Value.Split(',') select t.Trim()).Distinct()
})
.SelectMany((a, b) => a.Tags, (a, b) => new { Word = a.Word, Tag = b })
.GroupBy(e => e.Tag, StringComparer.InvariantCultureIgnoreCase)
.Select(e => new KeyValuePair<string, List<string>>(e.Key, e.Select(x => x.Word).ToList()))
.ToDictionary(e => e.Key, e => e.Value);
Dictionary<string, List<string>> descriptionsByTag =
(from e in descriptions
select new
{
Word = e.Key,
Tags = (from t in e.Value.Split(',') select t.Trim()).Distinct()
})
.SelectMany((a, b) => a.Tags, (a, b) => new { Word = a.Word, Tag = b })
.GroupBy(e => e.Tag, StringComparer.InvariantCultureIgnoreCase)
.Select(e => new KeyValuePair<string, List<string>>(e.Key, e.Select(x => x.Word).ToList()))
.ToDictionary(e => e.Key, e => e.Value);
var result = (from n in nounsByTag
join d in descriptionsByTag on n.Key equals d.Key
select new
{
Nouns = n.Value,
Descriptions = d.Value
}).ToList();
foreach (var item in result)
{
foreach (string noun in item.Nouns)
{
foreach (string desc in item.Descriptions)
{
Console.WriteLine("{0} {1}", noun, desc);
}
}
}结果
man is a word
woman is a word
cat is a word
house is a word
rat is a word
prison is a word
man alive
man is an animal
woman alive
woman is an animal
cat alive
cat is an animal
rat alive
rat is an animal
woman is female
house is made of bricks
prison is made of brickshttps://stackoverflow.com/questions/30098560
复制相似问题