存储多个真假值列表的最佳方法 [英] Best way to store multiple lists of true false values
问题描述
这只是为了解决好奇心 - 假设,在我的 C# 项目中,我有一个包含数百万个字符串的列表,每个字符串如下:
This is just to settle a curiosity - Suppose, in my C# project, I have a list containing millions of strings, each along the following lines:
"123Hi1234Howdy"
"Hi1Howdy23"
....
我需要知道的是,对于字符串中的每个字符,它是数字还是字母.
And all I need to know is, for each character in the string, if it is a digit or is it a letter.
所以,我认为存储它的最简单方法是 0 和 1 或 True/False.因此,在上面的示例中,假设我可以分配 IsLetter = 1
和 IsDigit = 0
,我可以将每一行转换为:
So, I was thinking the easiest way to store this would be as 0's and 1's or True / False. So, in the example above, assuming I could assign IsLetter = 1
and IsDigit = 0
, I could transform each line to:
"123Hi1234Howdy" >> 00011000011111
"Hi1Howdy23" >> 1101111100
....
这在我看来是存储我正在寻找的数据的最有效方式(但如果我错了,请已经纠正我 - 我仍然几乎编程新手).
That seems to me to be the most efficient way to store the data I'm looking for (but please do already correct me if I'm wrong on this - I'm still pretty much a newbie with programming).
因此,编写循环一行并检查每个字符是数字还是字母并将其转换为 true/false 或 1/0 的代码非常简单.我的问题是存储每行输出的最佳方式是什么?
So, writing the code that loops through a line and checks for whether each character is a digit or a letter and converting it to true/false or 1/0 is easy enough. My question is what would be the best way to store each line's output?
我应该将每一行的输出存储为一个位数组吗?它是否可以存储为其他类型(例如整数),然后可以转换回一系列位?它应该存储为布尔数组吗?关于存储这个的最佳方式还有其他想法吗?当这一切都说完了,我需要有一个我可以知道的清单,例如:
Should I store each line's output as a bit array? Could it be stored as some other type (maybe, say, integer) that could then be converted back to a series of bits? Should it be stored as a boolean array? Any other thoughts on the best way to store this? When it's all said and done, I need to have a list where I can know, for example:
myList[0] = 00011000011111
myList[1] = 1101111100
然后,因此 myList[0] <>myList[1]
推荐答案
您可以为每个单词使用 BitArray
并将位设置为 true 或 false,如果它们是数字或不是数字.请参阅此可能的解决方案:
You could use a BitArray
for each word and set the bits to true or false if they are a digit or not. See this possible solution:
void Main()
{
string[] words =
{
"123Hi1234Howdy",
"Hi1Howdy23"
};
//Create an array of BitArray
var bArrays = words.Select(w => new BitArray(w.Select(c => char.IsDigit(c)).ToArray()));
//You can also create string too
var strings = words.Select(w => new string(w.Select(c => char.IsDigit(c) ? '1' : '0').ToArray())).ToArray();
}
这不一定是最快或最有效的.我想这取决于您打算对字符串做什么,但至少它很简单!
This is not necessarily the fastest or most efficient. I guess it depends on what you intend to do with the strings, but at least it's simple!
这篇关于存储多个真假值列表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!