通过遍历数据来连接元素

2024-09-22 18:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在表格中有一些数据:

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)

加入规则:

  1. 组中所有元素的值相等

  2. 元素一的A等于元素二的B(或元素二的B,但不是A=A'或B=B')

  3. 最困难的是:将构成一系列相交节点的所有顺序数据分配给同一组

也就是说,第一个元素[1 2 5]必须与[2 2 3 5]连接,然后与[4 3 5 5]连接

当遍历大量数据时,您知道如何稳健地实现这一点吗?我对第三条规则有问题,其他规则很容易应用。对于有限的数据,我取得了一些成功,但这取决于我开始检查数据的顺序。这对大型数据集不起作用。 我可以使用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)

shapefile form and dbf form中的一些数据


Tags: 数据node元素newiforderinnerouter
1条回答
网友
1楼 · 发布于 2024-09-22 18:30:44

使用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

果然

相关问题 更多 >