【发布时间】:2011-08-09 20:56:17
【问题描述】:
假设我有一个如下所示的文件:
R34 128590 -74.498 109.728 0 0805_7
R33 128590 -74.498 112.014 0 0805_7
R15 128588 -68.910 127.254 0 0805_7
R32 128587 -65.354 115.189 0 0805_7
R35 128587 -65.354 117.348 0 0805_7
R38 128590 -65.354 119.507 0 0805_7
我想要做的是将 第 2 列 添加到列表中,并让计数器计算该项目出现的次数,并将其与数字一起输出,然后输出该数字的计数。
有没有办法使用列表来做到这一点?如果是这样,我该怎么做?
我尝试过搞乱事情,这就是我要去的地方..但它不能正常工作
int lineCount = 1;
int itemCounter = 0;
foreach (var item in aListBox.Items)
{
// Creates a string of the items in the ListBox.
var newItems = item.ToString();
// Replaces any multiple spaces (tabs) with a single space.
newItems = Regex.Replace(newItems, @"\s+", " ");
// Splits each line by spaces.
var eachItem = newItems.Split(' ');
###
### HERE is where I need help ###
###
List<string> partList = new List<string>();
partList.Add(eachItem[1]);
if (partList.Contains(eachItem[1]))
itemCounter++;
else
partList.Add(eachItem[1]);
sw.WriteLine(lineCount + ": "+ partList + ": " + itemCounter);
lineCount++;
}
SO 对于上面的示例,它会输出:
1: 128590: 3 #lineCount, partList, itemCounter
2: 128588: 1
3: 128587: 2
有人可以帮我弄清楚如何正确地做到这一点吗?
【问题讨论】: