我有一个字符串格式的数据集。这些是我通过图像处理获得的一些坐标。我附加了提取这些(坐标)并存储在单个数组中的代码。从这些坐标中,我发现x值相似。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stack
{
class Program
{
static void Main(string[] args)
{
int count = 0;
//String which have my coordinates
string TextFile = "[(27, 37), (27, 38), (27, 163), (27, 164), (27, 266), (27, 267), (27, 356), (27, 357), (27, 448), (27, 449), (27, 528), (27, 529), (28, 37), (28, 38), (28, 163), (28, 164), (28, 266), (28, 267), (28, 356), (28, 357), (28, 448), (28, 449), (28, 528), (28, 529), (51, 163), (51, 164), (51, 266), (51, 267), (51, 356), (51, 357), (51, 448), (51, 449), (52, 61), (52, 62), (52, 163), (52, 164), (52, 266), (52, 267), (52, 280), (52, 305), (52, 306), (52, 356), (52, 357), (52, 448), (52, 449), (52, 504), (52, 505), (53, 61), (53, 62), (53, 280), (53, 281), (53, 305), (53, 306), (53, 504), (53, 505), (355, 61), (355, 62), (355, 280), (355, 281), (355, 305), (355, 306), (355, 504), (355, 505), (356, 61), (356, 62), (356, 280), (356, 281), (356, 305), (356, 306), (356, 504), (356, 505), (380, 37), (380, 38), (380, 528), (380, 529), (381, 37), (381, 38), (381, 528), (381, 529)]";
string splits = TextFile.TrimStart('[');
string[] split = TextFile.Split(')');
string split_1 = null;
string split_2 = null;
string split_3 = null;
int pos = 0;
int lengthOfString = 0;
int valX, valZ, valX1, valZ1 = 0;
/*
* Getting the count of the coordinates in the array
*/
foreach (string x in split)
{
count++;
}
string[] stringArr = new string[count];
/*
* Splitting the coordinates as x,y and store in an array
*/
foreach (string coord in split)
{
split_1 = coord;
split_1 = split_1.Trim('[');
split_1 = split_1.Trim('(');
split_1 = split_1.Trim(',');
split_1 = split_1.Trim(' ');
split_1 = split_1.Trim('(');
split_1 = split_1.TrimEnd(']');
stringArr[pos] = split_1;
pos++;
}
Console.WriteLine("Array Length " + stringArr.Length);
/*
* extracting simalar x coordinates
*/
Console.WriteLine("");
Console.WriteLine("-----------------extracting simalar x coordinates----------------");
Console.WriteLine("");
int indexX = 0;
int loopRunX = 0;
for (int a = 0; a < stringArr.Length - 1; a = indexX)
{
split_2 = stringArr[a];
lengthOfString = split_2.Length;
valX = int.Parse(split_2.Substring(0, split_2.IndexOf(',')));
valZ = int.Parse(split_2.Substring(split_2.IndexOf(' '), (lengthOfString - split_2.IndexOf(' '))));
int countx = 0;
for (int x1 = indexX; x1 < stringArr.Length - 1; x1++)
{
split_3 = stringArr[x1];
lengthOfString = split_3.Length;
valX1 = int.Parse(split_3.Substring(0, split_3.IndexOf(',')));
valZ1 = int.Parse(split_3.Substring(split_3.IndexOf(' '), (lengthOfString - split_3.IndexOf(' '))));
//Check for the simillar x in the text file we provide
if (valX == valX1)
{
countx++;
Console.WriteLine("X is " + valX + " and the coordinates which have simillar x ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1 + " Count is " + countx);
}
else
{
break;
}
}
loopRunX++;
indexX = indexX + countx;
//Console.WriteLine("Next Index to check onwards : " + indexX);
//Console.WriteLine("Looping Count : " + loopRunX);
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
我想将这些相似的x值存储在不同的数组中。 例如:值27出现12次,我想将这些值存储到一个单独的数组中。值28出现12次,与上面相同,它应该将所有28个值存储在一个数组中。 但是我不知道需要多少个数组。我知道一个解决方案可以动态地将这些相似的坐标存储在一个数组中吗?
答案 0 :(得分:2)
您可以使用LINQ GroupBy方法。
notifyDataSetChanged
结果:
class Program
{
static void Main(string[] args)
{
int count = 0;
//String which have my coordinates
string TextFile = "[(27, 37), (27, 38), (27, 163), (27, 164), (27, 266), (27, 267), (27, 356), (27, 357), (27, 448), (27, 449), (27, 528), (27, 529), (28, 37), (28, 38), (28, 163), (28, 164), (28, 266), (28, 267), (28, 356), (28, 357), (28, 448), (28, 449), (28, 528), (28, 529), (51, 163), (51, 164), (51, 266), (51, 267), (51, 356), (51, 357), (51, 448), (51, 449), (52, 61), (52, 62), (52, 163), (52, 164), (52, 266), (52, 267), (52, 280), (52, 305), (52, 306), (52, 356), (52, 357), (52, 448), (52, 449), (52, 504), (52, 505), (53, 61), (53, 62), (53, 280), (53, 281), (53, 305), (53, 306), (53, 504), (53, 505), (355, 61), (355, 62), (355, 280), (355, 281), (355, 305), (355, 306), (355, 504), (355, 505), (356, 61), (356, 62), (356, 280), (356, 281), (356, 305), (356, 306), (356, 504), (356, 505), (380, 37), (380, 38), (380, 528), (380, 529), (381, 37), (381, 38), (381, 528), (381, 529)]";
string splits = TextFile.TrimStart('[');
string[] split = TextFile.Split(')');
string split_1 = null;
string split_2 = null;
string split_3 = null;
int pos = 0;
int lengthOfString = 0;
int valX, valZ, valX1, valZ1 = 0;
/*
* Getting the count of the coordinates in the array
*/
foreach (string x in split)
{
count++;
}
string[] stringArr = new string[count];
/*
* Splitting the coordinates as x,y and store in an array
*/
foreach (string coord in split)
{
split_1 = coord;
split_1 = split_1.Trim('[');
split_1 = split_1.Trim('(');
split_1 = split_1.Trim(',');
split_1 = split_1.Trim(' ');
split_1 = split_1.Trim('(');
split_1 = split_1.TrimEnd(']');
stringArr[pos] = split_1;
pos++;
}
Console.WriteLine("Array Length " + stringArr.Length);
//It is better to convert to Point type but to be short I've used your string
var groupedPoints = stringArr.GroupBy(
str => str.Split(',')[0]);
foreach (var group in groupedPoints)
{
Console.WriteLine("Group {0}", group.Key);
foreach (var point in group)
Console.WriteLine("\tPoint {0}", point);
}
Console.ReadLine();
}
}
}
此示例基于您的代码。但我建议用Array Length 80
Group 27
Point 27, 37
Point 27, 38
Point 27, 163
Point 27, 164
Point 27, 266
Point 27, 267
Point 27, 356
Point 27, 357
Point 27, 448
Point 27, 449
Point 27, 528
Point 27, 529
Group 28
Point 28, 37
Point 28, 38
Point 28, 163
Point 28, 164
Point 28, 266
Point 28, 267
Point 28, 356
Point 28, 357
Point 28, 448
Point 28, 449
Point 28, 528
Point 28, 529
Group 51
Point 51, 163
Point 51, 164
Point 51, 266
Point 51, 267
Point 51, 356
Point 51, 357
Point 51, 448
Point 51, 449
Group 52
Point 52, 61
Point 52, 62
Point 52, 163
Point 52, 164
Point 52, 266
Point 52, 267
Point 52, 280
Point 52, 305
Point 52, 306
Point 52, 356
Point 52, 357
Point 52, 448
Point 52, 449
Point 52, 504
Point 52, 505
Group 53
Point 53, 61
Point 53, 62
Point 53, 280
Point 53, 281
Point 53, 305
Point 53, 306
Point 53, 504
Point 53, 505
Group 355
Point 355, 61
Point 355, 62
Point 355, 280
Point 355, 281
Point 355, 305
Point 355, 306
Point 355, 504
Point 355, 505
Group 356
Point 356, 61
Point 356, 62
Point 356, 280
Point 356, 281
Point 356, 305
Point 356, 306
Point 356, 504
Point 356, 505
Group 380
Point 380, 37
Point 380, 38
Point 380, 528
Point 380, 529
Group 381
Point 381, 37
Point 381, 38
Point 381, 528
Point 381, 529
方法编写自己的MyPoint2d
class \ struct。
因此,您将能够转换
TryParse
- &gt;string TextFile
然后按MyPoint2d[] arrPts
此外,您不必使用静态大小数组(MyPoint2d.x
)。 string[]
将为您提供更多自由(在分配数组之前无需计算大小)。