-3

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

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

现在我相信字典可以帮助我识别给定列中的唯一值,并将整数与出现的每个国家/地区相关联。然后创建我的新数组,它应该是 int(或 double)类型,我可以遍历整个数组并通过字典定义新数组。我需要对具有字符串值的每一列执行此操作。这似乎效率低下,有没有更好的方法?

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

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

鉴于:

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

我想获得一个“数字”矩阵,其中包含字符串值列
MAKE;VALUE;GENDER
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)感谢您到目前为止的回复。

4

2 回答 2

0

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

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 代码来说明你的意思吗?

于 2013-07-03T11:45:16.620 回答
0

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

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

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 
            */
        }
    }
}
于 2016-05-19T04:21:23.797 回答