什么是最好的数据类型.. [英] what is the best datatype for..

查看:68
本文介绍了什么是最好的数据类型..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要某种类型的数据类型,其中包含ID'和

列表的计数/频率。当我进行一些处理时,我得到了一个ID回来

我需要存储并准确计算ID列出的次数

。例如,我可能会得到这些

数字的列表:

1,4,6,23,6,2,54,1,6,23,2,4 ,1,6


我会一次获得这些数字,需要建立一些东西

就像

1,3

4,2

6,4

23,2

2,2

$


在上面的数字X中,Y:

X-是数字

Y-是数数字


然后我需要对Y(计数)列上的数据进行排序:

6,4

1,3

4,2

23,2

2,2

43

任何人都可以建议如何做到最好。任何建议或帮助都是非常感激的!


谢谢,


Calvert

解决方案

我会将它存储在一个字典< int中,其中键是id,

值是计数。


然后,当你需要对键进行排序时,我会通过

KeyValuePair实例进行枚举并对它们进行排序(Dictionary< TKey,TValueimplements

IEnumerable< ; KeyValuePair< TKey,TValue>>)。你可以简单地将它们放入一个

数组中,然后在Array类上调用静态Sort方法,为比较< Tdelegate传入一个

匿名方法(在此如果比较< KeyValuePair< int,int>>),它将比较基于

的数值。


您还可以使用带有id列和

count列的DataTable,然后只需将数据视图放在数据表上即

按计数排序。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< ca ********** @ gmail.com写信息

新闻:89 ********************************* * @ d61g2000 hsa.googlegroups.com ...


>我需要某种数据类型,其中包含ID'和
他们的计数/频率NCY。当我进行一些处理时,我得到了一个ID回来

我需要存储并准确计算ID列出的次数

。例如,我可能会得到这些

数字的列表:

1,4,6,23,6,2,54,1,6,23,2,4 ,1,6


我会一次获得这些数字,需要建立一些东西

就像

1,3

4,2

6,4

23,2

2,2

$


在上面的数字X中,Y:

X-是数字

Y-是数数字


然后我需要对Y(计数)列上的数据进行排序:

6,4

1,3

4,2

23,2

2,2

43

任何人都可以建议如何做到最好。任何建议或帮助都是非常感谢!


谢谢,


Calvert


11月19日上午9:53,calvert4r ... @ gmail.com写道:


我需要某种数据类型来保存ID'和

列表的数量/频率。当我进行一些处理时,我得到了一个ID回来

我需要存储并准确计算ID列出的次数

。例如,我可能会得到这些

数字的列表:

1,4,6,23,6,2,54,1,6,23,2,4 ,1,6


我会一次获得这些数字,需要建立一些东西

就像

1,3

4,2

6,4

23,2

2,2

$


在上面的数字X中,Y:

X-是数字

Y-是数数字


然后我需要对Y(计数)列上的数据进行排序:

6,4

1,3

4,2

23,2

2,2

43

任何人都可以建议如何做到最好。任何建议或帮助都是非常感谢!


谢谢,


Calvert


对于数据类型,一般的经验法则是,如果你计划使用它来进行一些计算(如加法,减法等),那么/>
那么它应该是某种数字类型(int,double,decimal)。

如果你只是要存储它,那么使用一个字符串。例如,

即使电话号码都是数字,你应该使用一个字符串

,因为你不用电话号码进行计算。


对于你的问题,我认为你最好的选择是使用通用的

字典。使用ID作为键,值作为计数。阅读

数组中的每个值,检查它是否已存在于

字典中。如果是,那么只需增加计数,否则,将

添加到字典中。


Dim values()As Integer = {1,4, 6,23,6,2,54,1,6,23,2,4,1,6}

Dim idCount As New Dictionary(Of String,Integer)()


For Each i As Integer In values''Check

数组中的每个值

如果idCount.ContainsKey(i.ToString())那么''如果字典中已经存在值



idCount(i.ToString)+ = 1

''递增计数价值


否则

''否则

idCount.Add(i.ToString(),1)''add

字典值

结束如果

下一页

希望这会有所帮助


Chris


非常感谢你们。这正是我想要的信息。
正在寻找。我现在要试一试。再次感谢。


I need to some sort of data type that will hold a listing of ID''s and
their counts/frequency. As I do some processing I get an ID back
which I need to store and keep an accurate count for how many times
that ID is listed. For example I might get a listing of these
numbers:
1,4,6,23,6,2,54,1,6,23,2,4,1,6

I will get those numbers one at a time and need to build something
like
1,3
4,2
6,4
23,2
2,2
43,1

In above number X,Y:
X- is the number
Y- is the count of the number

I then need to sort the data on the Y (the count) column:
6,4
1,3
4,2
23,2
2,2
43,1

Can anyone suggest how best to do this. Any suggestions or help is
much appreciated!

Thanks,

Calvert

解决方案

I would store this in a Dictionary<int, intwhere the key is the id and
the value is the count.

Then, when you need to sort the keys, I would enumerate through the
KeyValuePair instances and sort those (Dictionary<TKey, TValueimplements
IEnumerable<KeyValuePair<TKey, TValue>>). You can simply put them into an
array and then call the static Sort method on the Array class, passing in an
anonymous method for the Comparison<Tdelegate (which in this case, would
be Comparison<KeyValuePair<int, int>>) which would compare the values based
on the count.

You could also get away with using a DataTable with an id column and a
count column, and then just placing a data view on the data table which is
sorted by the count.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ca**********@gmail.comwrote in message
news:89**********************************@d61g2000 hsa.googlegroups.com...

>I need to some sort of data type that will hold a listing of ID''s and
their counts/frequency. As I do some processing I get an ID back
which I need to store and keep an accurate count for how many times
that ID is listed. For example I might get a listing of these
numbers:
1,4,6,23,6,2,54,1,6,23,2,4,1,6

I will get those numbers one at a time and need to build something
like
1,3
4,2
6,4
23,2
2,2
43,1

In above number X,Y:
X- is the number
Y- is the count of the number

I then need to sort the data on the Y (the count) column:
6,4
1,3
4,2
23,2
2,2
43,1

Can anyone suggest how best to do this. Any suggestions or help is
much appreciated!

Thanks,

Calvert



On Nov 19, 9:53 am, calvert4r...@gmail.com wrote:

I need to some sort of data type that will hold a listing of ID''s and
their counts/frequency. As I do some processing I get an ID back
which I need to store and keep an accurate count for how many times
that ID is listed. For example I might get a listing of these
numbers:
1,4,6,23,6,2,54,1,6,23,2,4,1,6

I will get those numbers one at a time and need to build something
like
1,3
4,2
6,4
23,2
2,2
43,1

In above number X,Y:
X- is the number
Y- is the count of the number

I then need to sort the data on the Y (the count) column:
6,4
1,3
4,2
23,2
2,2
43,1

Can anyone suggest how best to do this. Any suggestions or help is
much appreciated!

Thanks,

Calvert

As for data types, a general rule of thumb would be that if you plan
to do some calculations with it (like addition, subtraction, etc.)
then it should be some sort of numeric type (int, double, decimal).
If you are only going to store it, then use a string. For example,
even though a phone number is all digits, you should use a string
since you don''t do calculations with a phone number.

For you problem, I think your best bet would be to use a generic
dictionary. Use the ID as the key and the value as the count. Read
each value in the array, check to see if it already exists in the
dictionary. If it does, then just increment the count, otherwise, add
it to the dictionary.

Dim values() As Integer = {1,4,6,23,6,2,54,1,6,23,2,4,1,6}
Dim idCount As New Dictionary(Of String, Integer)()

For Each i As Integer In values ''Check
each value in the array
If idCount.ContainsKey(i.ToString()) Then ''If the value
already exists in the dictionary
idCount(i.ToString) += 1
''increment the count for that value

Else
''otherwise
idCount.Add(i.ToString(), 1) ''add
the value to the dictionary
End If
Next
Hope this helps

Chris


Thank you both very much. That was exactly the information I was
looking for. I am going to give it a shot now. Thanks again.


这篇关于什么是最好的数据类型..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆