C#:获取字符串数组的整数表示形式

本文关键字:整数 表示 数组 获取 字符串 | 更新日期: 2023-09-27 18:20:47

我是C#的新手,遇到了以下问题(我在这里和谷歌上寻找了解决方案,但没有成功):

给定一个字符串数组(有些列可能是"字符串格式"的双精度或整数),我想将此数组转换为整数数组。这个问题只涉及具有实际字符串值的列(比如国家列表)。

现在,我相信字典可以帮助我识别给定列中的唯一值,并将一个整数与出现的每个国家关联起来。然后,为了创建类型为int(或double)的新数组,我可以遍历整个数组,并通过字典定义新数组。对于每一个有字符串值的列,我都需要这样做。这似乎效率低下,有更好的方法吗?

最后,我想对数据进行多元线性回归(甚至拟合一个广义线性模型,这意味着我想最终得到一个设计矩阵)。

编辑:1) 很抱歉不清楚,我会尽力澄清:

给定:

制造;价值;性别
奥迪;40912.2;m
WV;3332;f
奥迪;1234.99;m
DACIA;0;m
奥迪;12354.2;m
奥迪;123;m
大众;21321.2;f

我想得到一个"数字"矩阵,其中包含字符串值列的标识符
制造;价值;性别
1.40912.2;0
2.3332;1
1.1234.99;0
3.0;0
1.12354.2;0
1.123;0
2.21321.2;1

2) 我认为这实际上不是我解决问题所需要的。尽管如此,这似乎还是一个有趣的问题。

3) 感谢您迄今为止的回复。

C#:获取字符串数组的整数表示形式

这将获取所有可能的表示整数的字符串,并将它们放入列表中。您可以对表示双精度的字符串执行同样的操作。这就是你的意思吗??

List<int> myIntList = new List<int>()
foreach(string value in stringArray)
{
      int myInt;
      if(Int.TryParse(value,out myInt)
      {
            myIntList.Add(myInt);
      }
}

如果你想把每个字符串映射到一个像这样的键,字典是很好的:

var myDictionary = new Dictionary<int,string>();
myDictionary.Add(1,"CountryOne");
myDictionary.Add(2,"CountryTwo");
myDictionary.Add(3,"CountryThree");

然后你可以得到你的价值观,比如:

string myCountry = myDictionary[2];

但还是不确定我现在是否在帮助你。你有som代码来说明你的意思吗?

我不确定这是否是您想要的,但它确实输出了您想要的结果,您可以从中创建适当的数据结构来使用。我使用一个字符串列表,但您可以使用其他东西来保存处理后的数据。如果需要,我可以进一步扩展。

它确实假设基于分号字符的"列"的数量在整个数据中是相等的,并且足够灵活,可以处理任何数量的列。它有点难看,但它应该得到你想要的。

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication3
{
    class StringColIndex
    {
        public int ColIndex { get; set; }
        public List<string> StringValues {get;set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            var StringRepresentationAsInt = new List<StringColIndex>();
            List<string> rawDataList = new List<string>();
            List<string> rawDataWithStringsAsIdsList = new List<string>();
            rawDataList.Add("AUDI;40912.2;m");rawDataList.Add("VW;3332;f ");
            rawDataList.Add("AUDI;1234.99;m");rawDataList.Add("DACIA;0;m");
            rawDataList.Add("AUDI;12354.2;m");rawDataList.Add("AUDI;123;m");
            rawDataList.Add("VW;21321.2;f ");
            foreach(var rawData in rawDataList)
            {
                var split = rawData.Split(';');
                var line = string.Empty;
                for(int i= 0; i < split.Length; i++)
                {
                    double outValue;
                    var isNumberic = Double.TryParse(split[i], out outValue);
                    var txt = split[i];
                    if (!isNumberic)
                    {
                        if(StringRepresentationAsInt
                            .Where(x => x.ColIndex == i).Count() == 0)
                        {
                            StringRepresentationAsInt.Add(
                                new StringColIndex { ColIndex = i,
                                    StringValues = new List<string> { txt } });
                        }
                        var obj = StringRepresentationAsInt
                            .First(x => x.ColIndex == i);
                        if (!obj.StringValues.Contains(txt)){
                            obj.StringValues.Add(txt);
                        }
                        line += (string.IsNullOrEmpty(line) ? 
                            string.Empty : 
                            ("," + (obj.StringValues.IndexOf(txt) + 1).ToString()));
                    }
                    else
                    {
                        line += "," + split[i];
                    }
                }
                rawDataWithStringsAsIdsList.Add(line);
            }
            rawDataWithStringsAsIdsList.ForEach(x => Console.WriteLine(x));
            Console.ReadLine();
            /*
            Desired output:
            1;40912.2;0 
            2;3332;1 
            1;1234.99;0 
            3;0;0 
            1;12354.2;0 
            1;123;0 
            2;21321.2;1 
            */
        }
    }
}