【问题标题】:Replacing data from a list with its position将列表中的数据替换为其位置
【发布时间】:2016-09-14 00:12:36
【问题描述】:

我编写了一些代码,允许用户在富文本框中输入句子,然后数据将保存到列表中并删除重复项。

我想知道的是如何用它们的位置覆盖列表中的单词,然后用这些位置替换原始句子的位置。

例如:在句子Hello this is a test I hope this test works 中,该句子将被保存,删除重复项,并输出hello, this, is, a, test, I, hope, works,代码将其替换为1 2 3 4 5 6 7 8(我认为)。

现在我需要让程序用它在原始位置中的位置替换列表中的实际单词,这样它最终会说1 2 3 4 5 6 7 2 5 8,用逗号分隔。

这是我的代码:

string sentence = richTextBox1.Text;

list = sentence.Split(delimiterChars).ToList();
listoriginal = sentence.Split(delimiterChars).ToList();
listBox1.Items.Add("Full sentence: " + String.Join(" ", list));
list = list.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
listBox1.Items.Add("Words in the input: " + String.Join(", ", list));

for (int i = 0; i < list.Count; i++)
{
    list[i] = list[i].ToString();
    listoriginal[i] = listoriginal[i].ToString();
    resultList = listoriginal.Select(x => x.Replace(listoriginal[i], list[i])).ToList();
    i++;
}

listBox1.Items.Add("Final result: " + String.Join(", ", resultList));

【问题讨论】:

    标签: c# list listbox


    【解决方案1】:

    最简单的方法可能是使用像字典一样的键/值存储 - 一旦消除了重复项,您就可以将键/值推入字典并使用它来重新构建句子。

    // Create dictionary
    var dict = new Dictionary<string, int>();
    
    // When looping through the words to determine index add each word to the dict - the word being the key and the value being the index
    dict.Add(word, index);
    

    然后

    foreach(var word in words) {
        // Get the value from the dictionary for the associated key (the key being the word)
        var index = dict[word];
        // do stuff with index
    }
    

    您可以使用字符串连接来重新构建字符串而不是替换(因为您正在索引所有单词)。

    当您担心性能/内存时,通常的建议是使用StringBuilder,因为字符串是不可变的

    var sb = new StringBuilder();
    
    foreach(var word in words) {
        sb.Append(dict[word]);
        sb.Append(" ");
    }
    
    sb.ToString();
    

    编辑:

    这里有一个更完整的例子...

    var sentence = "hello world this is a test hello world";
    
    var words = sentence.Split(' ');
    var distinctWords = words.Distinct(StringComparer.InvariantCultureIgnoreCase);
    var dict = new Dictionary<string, int>();
    var ix = 0;
    foreach (var word in distinctWords)
    {
        dict.Add(word.ToLower(), ix++);
    }
    
    var sb = new StringBuilder();
    foreach (var word in words)
    {
        sb.Append(dict[word.ToLower()]);
        sb.Append(" ");
    }
    
    // sb.ToString(); 
    // 0 1 2 3 4 5 0 1
    

    显然,由于我没有进行替换,这可能会影响原始字符串的格式,但它会给你一个想法。您可以使用替换,但它会慢很多 - 但这取决于字符串的长度和您正在执行的处理量。

    【讨论】:

    • 我正在尝试使用您的解决方案,但是我将如何获得单词和索引,单词是 list[i] 还是我错了,至于索引,我只是不确定那是什么是
    • 当您使用 distinct 扩展方法提取不同的单词列表时,循环遍历它们并将每个单词推入字典中,并使用递增计数器(从 0 开始)。这是您可以用来构建句子等的键控列表。
    【解决方案2】:

    基本上,您需要动态填充 Dictionary&lt;string, int&gt; 来保存单词的结果位置。然后可以从Keys属性中获取结果词:

    var originalWords = sentence.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
    var uniqueWordPositions = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
    var originalWordPositions = new List<int>();
    foreach (var word in originalWords)
    {
        int position;
        if (!uniqueWordPositions.TryGetValue(word, out position))
            uniqueWordPositions.Add(word, position = uniqueWordPositions.Count + 1);
        originalWordPositions.Add(position);
    };
    listBox1.Items.Add("Full sentence: " + string.Join(" ", originalWords));
    listBox1.Items.Add("Words in the input: " + string.Join(", ", uniqueWordPositions.Keys));
    listBox1.Items.Add("Final result: " + string.Join(", ", originalWordPositions));
    

    【讨论】:

      【解决方案3】:

      使用已解决的单词创建字典。这意味着您将拥有以下内容:

      Dictionary<int, string> words = new Dictionary<int, string>();
      words.Items.Add(//number, //word);
      

      (你好,1) (这个,2) (是,3) (一,4) ......等等

      然后您可以编写一个方法来搜索是否已经存储了一个单词,例如:

      foreach(KeyValuePair<int, string> set in words)
      {
          if(set.value == //whatever word is next)
          {
              //write the number in the dictionary here
              //the corresponding number can be grabbed using: set.key
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-06
        • 1970-01-01
        • 1970-01-01
        • 2019-03-23
        相关资源
        最近更新 更多