C# 使两个列表中的单词匹配

C# 使两个列表中的单词匹配,c#,list,C#,List,我有两个列表,希望生成一个结果,其中单词将在逻辑上连接起来。例如:“人是活着的”,“老鼠是动物”等等 列表名词=新列表{男人[1],女人[2],猫[3],房子[4],老鼠[5],监狱[6]}; 列表描述=新列表{是活的[1][2][3][5],是砖块[4][6],是动物[3][5],是建筑物[4][6],是女性[2][3][5],是单词[1][2][3][4][5]; 据我所知,我需要首先使用这些列表填充哈希表?我该怎么做?我应该先在列表中添加特殊索引吗 我将其视为一个哈希表,其中所有的第一个

我有两个列表,希望生成一个结果,其中单词将在逻辑上连接起来。例如:“人是活着的”,“老鼠是动物”等等

列表名词=新列表{男人[1],女人[2],猫[3],房子[4],老鼠[5],监狱[6]};
列表描述=新列表{是活的[1][2][3][5],是砖块[4][6],是动物[3][5],是建筑物[4][6],是女性[2][3][5],是单词[1][2][3][4][5];
据我所知,我需要首先使用这些列表填充哈希表?我该怎么做?我应该先在列表中添加特殊索引吗

我将其视为一个哈希表,其中所有的第一个单词都有索引,如Man(1)、woman(2)等,然后所有描述只匹配正确的单词,如“is live(1),is a word(1)”。“是活的(2),是雌的(2),是活的(2)”。我想知道怎么做。

Code
如果没有逻辑,只有用于连接列表的索引,则可以使用
Enumerable.Zip

IEnumerable<string> result = nouns
    .Zip(descriptions, (n, d)=> String.Format("{0} {1}", n, d));

现在,您的问题已经改变,您已经澄清了一些事情,您似乎不想要两个列表,而是一个
字典
。因此,该值包含属于该名词的字符串的所有索引

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]);
var nounomdescriptions=新字典
{
{“人”,新名单{0,1,2,4},
// .....
};
foreach(变压器中的var kv)
foreach(单位为千伏值的整数指数)
Console.WriteLine(“{0}{1}”),kv.Key,descriptions[index]);
List temp=new List();
if(nomess.Count==descriptions.Count)
{
for(int i=0;i
或使用字典:

Dictionary<string, string> dict = new Dictionary<string, string>();
if (nouns.Count == descriptions.Count)
{
    for (int i = 0; i < nouns.Count; i++)
    {
        dict.Add(nouns[i], descriptions[i]);
    }
}
Dictionary dict=new Dictionary();
if(nomess.Count==descriptions.Count)
{
for(int i=0;i
您可以创建一个以名词为键的词典

var dictionary = new Dictionary<string,string>();
dictionary.Add("man","is alive");// "man" as key and "is alive" is value
.......
string description = dictionary["man"]; // the result will be "is alive"
var dictionary=newdictionary();
字典。添加(“人”,“活着”);//“人”是关键,“活着”是价值
.......
字符串描述=字典[“人”];//结果将是“还活着”
解析的可能解决方案(如果格式正确):

结果是

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 

你想用什么逻辑来连接这些列表呢?“女人是砖头做的”——那太好了。用更严肃的语气来说,你的两个列表看起来不像是索引对齐的,以适应你正在做的事情,这意味着你需要从语义上理解老鼠是动物。这是关于人工智能的问题吗?请把你的任务具体化。我想用我自己的逻辑。我想创建一些规则,这样我就不会用特殊的索引得到“女人是由砖块组成的”。好吧,如果你有自己的规则,你需要向我们展示你试图做的事情。你的问题太笼统了。这显然不是OP想要的。+1从未听说过@YuvalItzchakov:我不知道。他到底想要什么?编辑:现在我看到了评论,但我仍然不知道他到底想要什么。看看他最新的问题。他在问如何实现名词和描述之间的语义知识。@YuvalItzchakov:他可以使用
字典
,而列表包含其他列表中的所有索引。或
字典
,其中值列表存储了冗余描述。我已经编辑了我的答案。
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]);
List<string> temp = new List<string>();
if(nouns.Count == descriptions.Count)
{
    for(int i = 0; i < nouns.Count; i++)
    {
        temp.Add(string.format("{0} {1}", nouns[i], descriptions[i]));
    }
}
Dictionary<string, string> dict = new Dictionary<string, string>();
if (nouns.Count == descriptions.Count)
{
    for (int i = 0; i < nouns.Count; i++)
    {
        dict.Add(nouns[i], descriptions[i]);
    }
}
var dictionary = new Dictionary<string,string>();
dictionary.Add("man","is alive");// "man" as key and "is alive" is value
.......
string description = dictionary["man"]; // the result will be "is alive"
  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