繁体   English   中英

使用 Python 定义时间循环

[英]Defining a time loop using Python

此代码使用给定标准扫描相邻元素。 例如,它从
0.26373153 并选择 0.58720689,因为标准表明 select 个元素小于 0.6。 同样,它从 0.58720689 移动到 0.54531058。 当前的 output 以及所需的 output 随附。

我如何为每次迭代获得时间 output? 假设时间循环从 t=0 开始,表示 0.26373153,然后 t>0 表示下一个值:0.58720689,0.54531058,...

import numpy as np


def get_neighbor_indices(position, dimensions):
    '''
    dimensions is a shape of np.array
    '''
    i, j = position
    indices = [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]
    return [
        (i,j) for i,j in indices
        if i>=0 and i<dimensions[0]
            and j>=0 and j<dimensions[1]
        ]

def iterate_array(init_i, init_j, arr, condition_func):
    '''
    arr is an instance of np.array
    condition_func is a function (value) => boolean
    '''
    indices_to_check = [(init_i,init_j)]
    checked_indices = set()
    result = []
    while indices_to_check:
        pos = indices_to_check.pop()
        if pos in checked_indices:
            continue
        item = arr[pos]
        checked_indices.add(pos)
        if condition_func(item):
            result.append(item)
            indices_to_check.extend(
                get_neighbor_indices(pos, arr.shape)
            )
    return result


#P1 = np.random.rand(10,10)
 
P1=np.array([[ 1.40591794,  0.26373153,  0.98327887, 11.26958535,  1.25191783],
       [ 0.54531058,  0.58720689,  0.54674676,  3.89351201,  3.73486589],
       [ 0.50904881,  0.16939308,  0.27069582,  0.61941143,  0.88792361],
       [ 0.61828522,  0.30061379,  0.62551028,  0.28315714,  0.989013  ],
       [ 0.39175302,  0.30969749,  1.59701676,  2.11862101,  0.81709991]])

T=iterate_array(0,1, P1, lambda x : x < 0.6)
print(T)

当前output是

[0.26373153, 0.58720689, 0.54531058, 0.50904881, 0.16939308, 0.27069582, 0.54674676, 0.30061379, 0.30969749, 0.39175302]

此外,所需的 output 以及每个值的时间戳应如下所示。 这些值只是为了演示,因为我真的不知道实际的时间循环会显示什么。

[0,0.01,0.013,....] 

我不确定你需要什么。 如果您需要计算代码在看到另一个小于 0.6 的值之前尝试了多少次,这可以很容易地创建一个变量,该变量在每次迭代时增加并在您找到“正确”值时设置为 0。 如果您需要计算它运行了多少时间才能找到另一个小于 0.6 的值,则需要使用time库。 istruction t1 = time.time()在 t1 中保存自纪元以来经过的秒数(纪元是 1970 年 1 月 1 日,00:00:00)作为浮点数(更多详细信息请参见此处),因此当您开始研究你将时间保存在一个变量中(比如 t1),当你在数组中找到一个小于 0.6 的值时,你将当前时间保存在另一个变量中(比如 t2),此时就知道经过了多少时间您需要计算 (t2-t1); 然后将t2的值保存在t1中,如此继续。

编辑:通过您的编辑,我了解了您的需求。 试试这个,但记得添加import time

def iterate_array(init_i, init_j, arr, condition_func):
    '''
    arr is an instance of np.array
    condition_func is a function (value) => boolean
    '''
    indices_to_check = [(init_i,init_j)]
    checked_indices = set()
    result = []
    t0 = None
    t1 = None
    timestamps = []
    while indices_to_check:
        pos = indices_to_check.pop()
        if pos in checked_indices:
            continue
        item = arr[pos]
        checked_indices.add(pos)
        if condition_func(item):
            result.append(item)
            t1=time.time()
            if(t0==None):
                t0=t1
            timestamps.append(t1-t0)
            indices_to_check.extend(
                get_neighbor_indices(pos, arr.shape)
            )
    return result,timestamps

....

T,timestamps=iterate_array(0,1, P1, lambda x : x < 0.6)
print(T)
print(timestamps)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM