簡體   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