繁体   English   中英

如何用另一个数组中的值替换索引

[英]How to replace the indices with the value from another array

如您在以下脚本中所看到的,计算results.Add(dotproduct(userseq, z)); ,它将结果存储在列表中,根据值对列表进行排序,并在排序之前显示值的结果索引(原始索引),并在排序之前显示如下值:

0  0.235
1  0.985
2  0.342
3  0.548
4  0.754

排序后:

1  0.985
4  0.754
3  0.548
2  0.342
0  0.235

现在,我必须将排序后的索引(排序后的索引)替换为另一个值。 我有一个数组(一维),必须将已排序项目的索引与该数组进行比较,如果索引相同,则读取该索引的值,并将其替换为在排序后被排序的索引。 喜欢

1  0.985
4  0.754
3  0.548
2  0.342
0  0.235

一维数组,索引是隐式的。

0  672534
1  234523
2  567808
3  876955
4  89457

最后的结果一定是

234523  0.985
89457   0.754
876955  0.548
567808  0.342

int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
//int userseq=Array.IndexOf(d, sc);
for (int yu = 0; yu <= 92161; yu++)
{
    int wer = d[yu];
    if (wer == sc)
    {
        int userseq = yu;
    }
}
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++)
{
    results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
    sb1.AppendFormat("{0}: {1}", resultwithindex.Index, resultwithindex.result);
    sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());

我会将您的值存储为KeyValuePair数据类型

// Let's create the list that will store our information
// Keys will be ints, values the doubles
var myList = new List<KeyValuePair<int, double>>();

/*
Here is where you will load the values as you desire
This is up to you! Just add them as KeyValuePair objects to your list
(234523, 0.985), (89457, 0.754), (876955, 0.548), (567808, 0.342)
*/
// For example, I'll just add one:
myList.Add(new KeyValuePair<int, double>(567808, 0.342));

// Once you have created your list of desired KeyValuePairs, let's sort them.
// This will sort from high -> low as your example showed
myList.Sort((x, y) => y.Value.CompareTo(x.Value));

因此,剩下的就是您可以随意处理的KeyValuePair<int, double>类型的排序列表。

...您可以在此处了解有关KeyValuePair类型的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM