簡體   English   中英

Python:在while循環中更新列表或數組

[英]Python: Updating a list or array within a while loop

我有一個numpy數組的numpy數組(很樂意使用numpy數組的列表),並且我想編輯整個數組。 更具體地說,我檢查(較大數組中的)數組是否共享值,如果共享,則從較小數組中刪除共享值。

我遇到的問題是,當我嘗試將修改后的數組重新插入到所有包含的數組中時,while循環完成時的最終輸出不記得更新后的模塊了。

我相信這與python復制/查看項的細微差別有關,當我訪問整個數組的元素i或j時,我在while循環中創建了一個新對象,而不是在較大數組中編輯該元素。 但是,我很高興地承認,我不完全了解這一點,盡管經過數小時的嘗試,但最終還是無法想到替代方案。

#Feature_Modules is an array (or list) of number arrays, each containing a set of integers
i = 0
j = 0
while i < Feature_Modules.shape[0]: # Check element i against every other element j
    if i != j:
        Ref_Module = Feature_Modules[i]
        while j < Feature_Modules.shape[0]:
            if i != j:
                Query_Module = Feature_Modules[j]
                if np.array_equal(np.sort(Ref_Module),np.sort(Query_Module)) == 1: # If modules contain exactly the same integers, delete one of this. This bit actually works and is outputted at the end.
                    Feature_Modules = np.delete(Feature_Modules,j)
                Shared_Features = np.intersect1d(Ref_Module, Query_Module)
                if Shared_Features.shape[0] > 0 and np.array_equal(np.sort(Ref_Module),np.sort(Query_Module)) == 0: # If the modules share elements, remove the shared elements from the smaller module. This is the bit that isn't outputted in the final Feature_Modules object.
                    Module_Cardinalities = np.array([Ref_Module.shape[0],Query_Module.shape[0]])
                    Smaller_Group = np.where(Module_Cardinalities == np.min(Module_Cardinalities))[0][0]
                    New_Groups = np.array([Ref_Module,Query_Module])
                    New_Groups[Smaller_Group] = np.delete(New_Groups[Smaller_Group],np.where(np.isin(New_Groups[Smaller_Group],Shared_Features) == 1))
                    Feature_Modules = Feature_Modules.copy()
                    Feature_Modules[i] = New_Groups[0] # Replace the current module of Feature_Modules with the new module (Isn't outputted at end of while loops)
                    Feature_Modules[j] = New_Groups[1] # Replace the current module of Feature_Modules with the new module (Isn't outputted at end of while loops)
                else:
                    j = j + 1
            else:
                j = j + 1
    else:
        i = i + 1
    i = i + 1

因此,如果以這個小的數據集為例,

Feature_Modules = np.array([np.array([1,2,3,4,5,6,7,8]),np.array([9,10,1,2,3,4]), np.array([20,21,22,23])])

新的Feature_Modules應該是;

Feature_Modules = np.array([np.array([1,2,3,4,5,6,7,8]), np.array([9,10]), np.array([20,21,22,23])])

因為數組[0]和[1]中的共享值已從[1]中刪除,因為它是較小的數組。

我建議對代碼使用更多的python X numpy方法:

import numpy as np

Feature_Modules = np.array([np.array([1,2,3,4,5,6,7,8]), np.array([9,10,1,2,3,4]), np.array([20,21,22,23])])

for n1,arr1 in enumerate(Feature_Modules[:-1]):
    l1 = len(arr1)
    for n2,arr2 in enumerate(Feature_Modules[n1+1:]):
        l2 = len(arr2)
        intersect, ind1, ind2 = np.intersect1d(arr1, arr2, return_indices=True)
        if len(intersect) == 0:
            continue
        if l1 > l2:
            Feature_Modules[n2+n1+1] = np.delete(arr2, ind2)
        else:
            Feature_Modules[n1] = np.delete(arr1, ind1)

# [array([1, 2, 3, 4, 5, 6, 7, 8]) array([ 9, 10]) array([20, 21, 22, 23])]

編輯:

此代碼將編輯原始數組,以跟蹤已刪除元素的列表。 如果要保留原始數組,請對其進行復制:

copy = np.array(original)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM