超大元组数组的快速迭代

2024-04-19 16:47:57 发布

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

我有一个很大的np.array元组。所以,它是这样的:

my_array=[(1,2,3),(1,3,5)]

有没有比itertool更快的方法来迭代数组,即it.chain方法


Tags: 方法chainmynpit数组array元组
1条回答
网友
1楼 · 发布于 2024-04-19 16:47:57

我稍微编辑了一下Python代码

我正在创造细胞自动机来模拟三维的肿瘤生长。每个肿瘤细胞都有其位置(细胞id)。在一次迭代中,每个肿瘤细胞可以:分裂、迁移或死亡

这是我的密码:

    keys = list(TC.keys())
    keys = np.array(keys)
    for cell in np.ndenumerate(keys): # for each tumor cell
       cell_id = cell[1]
       division, migration, death = TC[cell_id].action(TC_pdiv, TC_pmig, TC_pdeath)
       if death:
         DeadCells[cell_id] =  TC[cell_id] # remove tumor cell from dictionary
         continue
       else:
        # extract cell position
           x = TC[cell_id].x
           y = TC[cell_id].y
           z = TC[cell_id].z
        # find free spots where new cell can divide or cell can migrate
           ngh = TC[cell_id].freeSpots(cells, TC[cell_id].neighbors(x,y,z))
           if np.size(ngh)>0:
               ngh = np.reshape(ngh,[int(np.shape(ngh)[0]/3),3])
               PositionOfNewCell = ngh[random.randint(0,np.size(ngh)/3-1)]
               cell_id_new = int(x + (y - 1) * gridSize + (z - 1) * gridSize * gridSize)      

           else:
              continue

        if migration:
            TC[cell_id_new] = TC.pop(cell_id)
            continue

        if division:
            x = int(PositionOfNewCell[0])
            y = int(PositionOfNewCell[1])
            z = int(PositionOfNewCell[2])
            NewCells[cell_id_new] = TumorCell(x,y,z,TC_pcap)

10^4个单元格的代码大约需要60秒

我的目标是尽可能减少时间,因为我的目标是用10^6个细胞模拟细胞自动机

事先谢谢你的帮助

相关问题 更多 >