将字符串数组转换为二维数组

huangapple go评论63阅读模式
英文:

Convert string array to 2d Array

问题

以下是您要翻译的代码部分:

I have the following string array comprising of geo-coordinates

    32.28057860000054 -174.8307036999996,
    33.03439570061262 -174.82691533618038,
    33.15555942151208 -174.8255452254468,
    33.161189722126196 -174.825486109871,
    33.16405200490681 -174.82544919191812,
    33.22284105148913 -174.824784409563,
    33.32412188155387 -174.82338459780988,
    33.341040408962115 -174.8231663812863
    ...............
    
I want to convert the above to a 2d-array and also swap the values

e.g.

    {
       { -174.8307036999996, 32.28057860000054 },
       { -174.82691533618038, 33.03439570061262 }
    }

I know how to create a 2d-array from scratch 

    string[,] name = new string[,]
    {
          { "damien", "kettle" },
          { "joe", "bloggs" },
          { "pat", "murphy" },
          { "tom", "jones" }
    };

but not able to figure out how I can convert the original list of coordinates?

Suggestions please?
英文:

I have the following string array comprising of geo-coordinates

32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
...............

I want to convert the above to a 2d-array and also swap the values

e.g.

{
   { -174.8307036999996, 32.28057860000054 },
   { -174.82691533618038, 33.03439570061262 }
}

I know how to create a 2d-array from scratch

string[,] name = new string[,]
{
      { "damien", "kettle" },
      { "joe", "bloggs" },
      { "pat", "murphy" },
      { "tom", "jones" }
};

but not able to figure out how I can convert the original list of coordinates?

Suggestions please?

答案1

得分: 1

double[,] coordinates2D = new double[coordinates.Length, 2];

for (int i = 0; i < coordinates.Length; i++)
{
string[] parts = coordinates[i].Split(' ');
double lat = double.Parse(parts[0]);
double lon = double.Parse(parts[1]);
coordinates2D[i, 0] = lon;
coordinates2D[i, 1] = lat;
}

Or

// 将坐标拆分成字符串的交错数组
string[][] splitCoordinates = coordinates.Select(c => c.Split(' ')).ToArray();

// 创建一个包含双精度值的2D数组,并交换数值
double[,] result = new double[splitCoordinates.Length, 2];
for (int i = 0; i < splitCoordinates.Length; i++)
{
result[i, 0] = double.Parse(splitCoordinates[i][1]);
result[i, 1] = double.Parse(splitCoordinates[i][0]);
}

英文:
double[,] coordinates2D = new double[coordinates.Length, 2];

for (int i = 0; i &lt; coordinates.Length; i++)
{
    string[] parts = coordinates[i].Split(&#39; &#39;);
    double lat = double.Parse(parts[0]);
    double lon = double.Parse(parts[1]);
    coordinates2D[i, 0] = lon;
    coordinates2D[i, 1] = lat;
}

Or

// Split the coordinates into a jagged array of strings
string[][] splitCoordinates = coordinates.Select(c =&gt; c.Split(&#39; &#39;)).ToArray();

// Create a 2D array of doubles and swap the values
double[,] result = new double[splitCoordinates.Length, 2];
for (int i = 0; i &lt; splitCoordinates.Length; i++)
{
    result[i, 0] = double.Parse(splitCoordinates[i][1]);
    result[i, 1] = double.Parse(splitCoordinates[i][0]);
}

答案2

得分: 0

这部分代码的翻译如下:

也许这会起作用

var text = &quot;&quot;&quot;
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
&quot;&quot;&quot;;

var values = text.Split(&quot;,&quot;, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
    .Select(l =&gt; l.Split(&quot; &quot;))
    .Select(a =&gt; new double[] { double.Parse(a.Last()), double.Parse(a.First())});
英文:

Maybe this would work

var text = &quot;&quot;&quot;
32.28057860000054 -174.8307036999996,
33.03439570061262 -174.82691533618038,
33.15555942151208 -174.8255452254468,
33.161189722126196 -174.825486109871,
33.16405200490681 -174.82544919191812,
33.22284105148913 -174.824784409563,
33.32412188155387 -174.82338459780988,
33.341040408962115 -174.8231663812863
&quot;&quot;&quot;;

var values = text.Split(&quot;,&quot;, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
	.Select(l =&gt; l.Split(&quot; &quot;))
	.Select(a =&gt; new double[] { double.Parse(a.Last()), double.Parse(a.First())});

huangapple
  • 本文由 发表于 2023年4月4日 06:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定