小编FSm*_*FSm的帖子

将字符串拆分为具有特定数量元素的数组,c#

我有一个字符串,其中包含由行(\n)分隔的有序术语的数量,如下例所示:(注意,我所拥有的字符串是字符串数组的元素)

term 1
term 2
.......
.......
term n
Run Code Online (Sandbox Code Playgroud)

我想分割一定数量的术语,让我们只说(1000)并弃掉其余的术语.我正在尝试以下代码:

string[] training = traindocs[tr].Trim().Split('\n');
                List <string> trainterms = new List<string>();
                for (int i = 0; i < 1000; i++)
                {
                    if (i >= training.Length)
                        break;
                    trainterms.Add(training[i].Trim().Split('\t')[0]);
                } 
Run Code Online (Sandbox Code Playgroud)

我可以在不使用List或任何其他数据结构的情况下执行此操作吗?我的意思是直接将特定数量的术语提取到数组(训练)中?提前致谢.

c# c#-4.0

2
推荐指数
1
解决办法
3184
查看次数

交叉概率对遗传算法/遗传编程有什么影响?

任何人都可以给出交叉概率的例子吗?我想知道确定交叉概率有什么好处,以及它在遗传算法或遗传编程中有什么影响.

genetic-programming genetic-algorithm evolutionary-algorithm

1
推荐指数
1
解决办法
3547
查看次数

数组数组为一个数组c#

我正在尝试转换

string[][] allcats

into 

string[] ToOneArray 
Run Code Online (Sandbox Code Playgroud)

可以对快速Linq方式有任何帮助或建议吗?非常感谢

c#

1
推荐指数
1
解决办法
85
查看次数

数组不允许减去索引c#

我有以下程序来计算doctionary(freq)的每个条目的文档频率

  foreach (var termIndex in freq.Select(entry => GetTermIndex(entry.Key)))
         {                                                              
            _docFreq[i][termIndex]++;
         }
Run Code Online (Sandbox Code Playgroud)

以及获得期限索引的程序

rivate int GetTermIndex(string term)
    {
        int i;
        if (_wordsIndex.TryGetValue(term, out i))
            return i;
        else
            return 0;
    }
Run Code Online (Sandbox Code Playgroud)

其中,所有术语都在另一个字典(_wordsIndex)中编入索引.

基于上述过程,如果freq中的entry.Key不存在,那么GetTermIndex将返回0并且将计算_docFreq(_docFreq [i] [0]),这就是问题所在.那么,我怎么能避免在入口时计数.键不存在?我试着做点什么

rivate int GetTermIndex(string term)
    {
        int i;
        if (_wordsIndex.TryGetValue(term, out i))
            return i;
        else
            return -1;
    }
Run Code Online (Sandbox Code Playgroud)

但是当然出现了错误"索引超出了数组的范围",因为没有_docFreq [i] [ - 1]的索引.

可以帮忙吗?非常感谢

c# c#-4.0

1
推荐指数
1
解决办法
130
查看次数

JDK 10 中未重新调整 var 关键字

我已经成功安装了带有 JDK 10 的 netbeans(Apache 版本),但是var在我的项目中无法使用关键字,它一直说cannot find symbol. 任何帮助,将不胜感激。

java netbeans java-10

1
推荐指数
1
解决办法
3554
查看次数

Arraylist vs List vs Dictionary

谁能救我?我有以下代码:

private List<string> GenerateTerms(string[] docs)
{
    List <string> uniques = new List<string>();

    for (int i = 0; i < docs.Length; i++)
    {
        string[] tokens = docs[i].Split(' ');

        List<string> toktolist = new List<string>(tokens.ToList());

        var query = toktolist.GroupBy(word => word)
             .OrderByDescending(g => g.Count())
             .Select(g => g.Key)
             .Take(20000);              

        foreach (string k in query)
        {
            if (!uniques.Contains(k)) 
                uniques.Add(k);
        }
    }            

    return uniques;            
}
Run Code Online (Sandbox Code Playgroud)

它是基于最高频率从多个文档生成术语.我使用字典做了相同的程序.在这两种情况下花费了440毫秒.但令人惊讶的是,当我使用数组列表的过程时,如下面的代码

private ArrayList GenerateTerms(string[] docs)
{
    Dictionary<string, int> yy = new Dictionary<string, int>();
    ArrayList uniques = new ArrayList();

    for (int i …
Run Code Online (Sandbox Code Playgroud)

c# c#-4.0

0
推荐指数
1
解决办法
1668
查看次数

字典中的第一项

我有一个按降序排序的字典.每个字符串(键)是一个术语,int(值)是它的计数.我如何得到第一个计数?因为它指的是最大数量(频率).......提前感谢.

Just to inform some of whom commented that the dictionary<string,int> will change its
rank. Be sure, if you are grouping  the dictionary by its count, there is no problem  
with the order . Always the dictionary will come with highest count at first.
Run Code Online (Sandbox Code Playgroud)

c# c#-4.0

0
推荐指数
1
解决办法
4331
查看次数

如何将List <string>转换为Dictionary <string,int>?

如何将a List <string>转换为Dictionary<string, int>

这应该是从零到n的数字.

c# c#-4.0

0
推荐指数
1
解决办法
118
查看次数

循环遍历字典并获取每个值C#

我正在使用以下代码来获取字典及其索引的每个值

foreach (var termIndex in freq.Select(entry => GetTermIndex(entry.Key)))
{
     var wordFreq = entry.Value;
     ........
}
Run Code Online (Sandbox Code Playgroud)

为什么突然出错

当前上下文中不存在名称"entry"

出现??有人可以帮忙吗?

非常感谢

c# c#-4.0

0
推荐指数
2
解决办法
227
查看次数