我有一些数据,格式为:
ID A B VALUE EXPECTED RESULT
1 1 2 5 GROUP1
2 2 3 5 GROUP1
3 3 4 6 GROUP2
4 3 5 5 GROUP1
5 6 4 5 GROUP3
我想做的是遍历数据(数千行)并创建一个公共字段,这样我就可以轻松地加入数据(* A->开始节点,B->结束节点值->顺序...数据形成了类似链条的形式,其中只有邻居共享相同的A或B)
加入规则:
相等值
元素一的A等于元素二的B(或相反,但不是A = A'或B = B')
最困难的一个:将形成一系列相交节点的所有顺序数据分配给同一组。
这是第一个元素[1 1 2 5]必须先与[2 2 3 5]相连,然后再与[4 3 5 5]相连
有什么想法在遍历大量数据时如何稳健地做到这一点?我对第3条规则有疑问,其他规则也很容易应用。对于有限的数据,我取得了一些成功,但这取决于我开始检查数据的顺序。这不适用于大型数据集。 我可以使用arcpy(最好),甚至可以使用Python或R或Matlab来解决这个问题。 尝试过arcpy失败,因此我正在检查替代方案。
在ArcPy中,此代码可以正常运行,但扩展范围有限(即在具有很多细分的大型特征中,我得到3-4组而不是1组):
TheShapefile="c:/Temp/temp.shp"
desc = arcpy.Describe(TheShapefile)
flds = desc.fields
fldin = 'no'
for fld in flds: #Check if new field exists
if fld.name == 'new':
fldin = 'yes'
if fldin!='yes': #If not create
arcpy.AddField_management(TheShapefile, "new", "SHORT")
arcpy.CalculateField_management(TheShapefile,"new",'!FID!', "PYTHON_9.3") # Copy FID to new
with arcpy.da.SearchCursor(TheShapefile, ["FID","NODE_A","NODE_B","ORDER_","new"]) as TheSearch:
for SearchRow in TheSearch:
if SearchRow[1]==SearchRow[4]:
Outer_FID=SearchRow[0]
else:
Outer_FID=SearchRow[4]
Outer_NODEA=SearchRow[1]
Outer_NODEB=SearchRow[2]
Outer_ORDER=SearchRow[3]
Outer_NEW=SearchRow[4]
with arcpy.da.UpdateCursor(TheShapefile, ["FID","NODE_A","NODE_B","ORDER_","new"]) as TheUpdate:
for UpdateRow in TheUpdate:
Inner_FID=UpdateRow[0]
Inner_NODEA=UpdateRow[1]
Inner_NODEB=UpdateRow[2]
Inner_ORDER=UpdateRow[3]
if Inner_ORDER==Outer_ORDER and (Inner_NODEA==Outer_NODEB or Inner_NODEB==Outer_NODEA):
UpdateRow[4]=Outer_FID
TheUpdate.updateRow(UpdateRow)
中的一些数据
答案 0 :(得分:2)
使用matlab:
A = [1 1 2 5
2 2 3 5
3 3 4 6
4 3 5 5
5 6 4 5]
%% Initialization
% index of matrix line sharing the same group
ind = 1
% length of the index
len = length(ind)
% the group array
g = []
% group counter
c = 1
% Start the small algorithm
while 1
% Check if another line with the same "Value" share some common node
ind = find(any(ismember(A(:,2:3),A(ind,2:3)) & A(:,4) == A(ind(end),4),2));
% If there is no new line, we create a group with the discovered line
if length(ind) == len
%group assignment
g(A(ind,1)) = c
c = c+1
% delete the already discovered line (or node...)
A(ind,:) = []
% break if no more node
if isempty(A)
break
end
% reset the index for the next group
ind = 1;
end
len = length(ind);
end
这是输出:
g =
1 1 2 1 3
符合预期