简体   繁体   English

存储多个真假值列表的最佳方法

[英]Best way to store multiple lists of true false values

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: 这只是为了解决好奇心-假设在我的C#项目中,我有一个包含数百万个字符串的列表,每个字符串如下:

"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. 我需要知道的是,对于字符串中的每个字符,它是数字还是字母。

So, I was thinking the easiest way to store this would be as 0's and 1's or True / False. 因此,我认为最简单的存储方式是0和1或True / False。 So, in the example above, assuming I could assign IsLetter = 1 and IsDigit = 0 , I could transform each line to: 因此,在上面的示例中,假设我可以分配IsLetter = 1IsDigit = 0 ,则可以将每行转换为:

"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). 在我看来,存储所需数据的最有效方法(但是,如果我错了,请已经对我进行了纠正-我仍然是编程的新手)。

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. 因此,编写遍历一行并检查每个字符是数字还是字母并将其转换为true / false或1/0的代码很容易。 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

And, then, therefore myList[0] <> myList[1] 然后,因此, myList[0] <> myList[1]

You could use a BitArray for each word and set the bits to true or false if they are a digit or not. 您可以为每个单词使用一个BitArray ,如果位不是数字,则将它们设置为true或false。 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! 我想这取决于您打算如何处理字符串,但至少很简单!

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

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