使两个列表中的单词匹配

时间:2015-05-07 10:34:42

标签: c# list

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

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]};

据我所知,我需要首先使用这些列表填充哈希表?我该怎么做?我应该首先在列表中添加特殊索引吗?

我认为它是一些散列表,其中所有第一个单词都有索引,如Man(1),woman(2)等,然后所有描述只匹配正确的单词,如“is alive(1),是一个单词(1) ”。 “活着(2),是女性(2),活着(2)”。我想知道怎么做。

5 个答案:

答案 0 :(得分:4)

如果没有逻辑但您想用来连接列表的索引,可以使用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]); 。因此,该值包含属于名词的字符串的所有索引。

with src as 
(
  select 'O' as "MODE", 'V1234567890' as  Vehicle, '1411196232' as OrderCode, to_date('2014-11-19 16:34:35','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
  union all
  select 'I' as "MODE", 'V1234567890' as  Vehicle, '1411196232' as OrderCode, to_date('2014-11-19 15:27:09','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
  union all
  select 'O' as "MODE", 'V2987654321' as  Vehicle, '1411206614' as OrderCode, to_date('2014-11-20 14:03:02','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
  union all
  select 'I' as "MODE", 'V2987654321' as  Vehicle, '1411206614' as OrderCode, to_date('2014-11-20 13:47:02','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
  union all
  select 'O' as "MODE", 'V2987654321' as  Vehicle, '1411185798' as OrderCode, to_date('2014-11-20 01:40:58','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
  union all
  select 'I' as "MODE", 'V2987654321' as  Vehicle, '1411185798' as OrderCode, to_date('2014-11-20 00:47:02','yyyy-mm-dd hh24:mi:ss') as ActionDate from dual
)

select Vehicle,OrderCode, max(case when "MODE" = 'O' then ActionDate end) as MODE_O,  max(case when "MODE" = 'I' then ActionDate end) as MODE_I from src
group by Vehicle,OrderCode

答案 1 :(得分:2)

使用解析的可能解决方案(如果我的格式正确):

  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 

答案 2 :(得分:0)

代码

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 bricks

答案 3 :(得分:0)

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]);
    }
}

答案 4 :(得分:0)

您可以使用名词作为键创建一个词典

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"