计算数组中的重复项 [英] Count duplicated items in array
本文介绍了计算数组中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将AutoCAD属性块提取为excel
< a href =https://drive.google.com/file/d/1CF3n1O99Rz_rko4K89TVLB_8jLLdZE-M/view?usp=sharing> Excel输出 [ ^ ]
My问题是如何计算数组中重复的项目并将它们添加到新列
我尝试过:
列表< object []> rooms = new List< object []>();
using(var tr = db.TransactionManager.StartTransaction())
{
foreach(SelectedObject in psr.Value)
{
BlockReference bref =(BlockReference) tr.GetObject(so.ObjectId,OpenMode.ForRead);
Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcoll = bref.AttributeCollection;
object [] info = new object [4];
foreach(attcoll中的ObjectId id)
{
AttributeReference atref =(AttributeReference)tr.GetObject(id,OpenMode.ForRead);
if(atref.Tag ==ROOM)
{
info [0] = atref.TextString;
}
if(atref.Tag ==ROOMID)
{
info [1] = atref.TextString;
}
if(atref.Tag ==ROOMAREA)
{
info [2] = atref.TextString;
}
if(!rooms.Contains(info))
{
rooms.Add(info);
}
}
}
tr.Commit();
}
解决方案
而不是List< object []>
我使用词典 [ ^ ]。
字典< string,int> myCounter = new Dictionary< string,int>();
使用( var tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject so in psr.Value)
{
BlockReference bref =(BlockReference)tr.GetObject(so.ObjectId,OpenMode.ForRead);
Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcoll = bref.AttributeCollection;
foreach (ObjectId id in attcoll)
{
AttributeReference atref =(AttributeReference)tr.GetObject(id,OpenMode.ForRead);
if (atref.Tag.Contains( ROOM))
if (!myCounter.ContainsKey(atref.TextString))
{
myCounter。添加(atref.TextString, 1 );
}
else
{
myCounter [atref.TextString] + = 1 跨度>;
}
}
}
}
// Dictionary对象包含具有出现次数的唯一数据
Console.WriteLine( Key | Count);
foreach ( var k in myCounter.Keys)
{
Console.WriteLine( {0} | {1} ,k,myCounter [k]);
}
上面的代码应该返回如下内容:
键|数
ROOM | 5
ROOMID | 10
ROOMAREA | 15
I'm trying to extract AutoCAD attribute block to excel
Excel Output[^]
My question is how to count duplicated items in array and add them to new column
What I have tried:
List<object[]> rooms = new List<object[]>(); using (var tr = db.TransactionManager.StartTransaction()) { foreach (SelectedObject so in psr.Value) { BlockReference bref = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead); Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcoll = bref.AttributeCollection; object[] info = new object[4]; foreach (ObjectId id in attcoll) { AttributeReference atref = (AttributeReference)tr.GetObject(id, OpenMode.ForRead); if (atref.Tag=="ROOM") { info[0] = atref.TextString; } if (atref.Tag=="ROOMID") { info[1] = atref.TextString; } if (atref.Tag=="ROOMAREA") { info[2] = atref.TextString; } if (!rooms.Contains(info)) { rooms.Add(info); } } } tr.Commit(); }
解决方案
Instead ofList<object[]>
i'd use a Dictionary[^].
Dictionary<string, int> myCounter = new Dictionary<string, int>(); using (var tr = db.TransactionManager.StartTransaction()) { foreach (SelectedObject so in psr.Value) { BlockReference bref = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead); Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcoll = bref.AttributeCollection; foreach (ObjectId id in attcoll) { AttributeReference atref = (AttributeReference)tr.GetObject(id, OpenMode.ForRead); if (atref.Tag.Contains("ROOM")) if (!myCounter.ContainsKey(atref.TextString)) { myCounter.Add(atref.TextString, 1); } else { myCounter[atref.TextString] += 1; } } } } //Dictionary object contains unique data with the number of occurences Console.WriteLine("Key | Count"); foreach(var k in myCounter.Keys) { Console.WriteLine("{0} | {1}", k, myCounter[k]); }
Above code should return something like this:
Key | Count ROOM | 5 ROOMID | 10 ROOMAREA | 15
这篇关于计算数组中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文