Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于可视化循环ETA的进度条_Python_Python 3.x_For Loop - Fatal编程技术网

Python 用于可视化循环ETA的进度条

Python 用于可视化循环ETA的进度条,python,python-3.x,for-loop,Python,Python 3.x,For Loop,我的代码中有这一行 lev_similarity = -1*np.array([[distance.levenshtein(w1,w2) for w1 in words] for w2 in words]) 这需要很长时间,所以我想包括一个进度条与预计到达时间,以显示我估计的处理时间 在给定此内部循环的情况下,如何包含进度条 我的做法: npa = [] akt = 1 with progressbar.ProgressBar(max_value=len(words)) as bar:

我的代码中有这一行

lev_similarity = -1*np.array([[distance.levenshtein(w1,w2) for w1 in words] for w2 in words])
这需要很长时间,所以我想包括一个进度条与预计到达时间,以显示我估计的处理时间

在给定此内部循环的情况下,如何包含进度条

我的做法:

npa = []
akt = 1
with progressbar.ProgressBar(max_value=len(words)) as bar:
    for w2 in words:
        bar.update(akt)
        akt = akt + 1
        for w1 in words:
            npa.append(distance.levenshtein(w1,w2))
lev_similarity = -1*np.array(npa)
TQDM库()为python中的进度条提供了一个非常简单的接口

它会变成s.t.h.的样子:

from tqdm import tqdm

... code ...

new_arr = -1*np.array([[distance.levenshtein(w1,w2) for w1 in words] for w2 in tqdm(words)])

tqdm手册中有一节介绍嵌套循环。

您可以在循环中使用循环,而不是列表,并在内部或外部循环中更新进度栏。 如果仍然需要列表理解,可以定义一个函数,该函数既执行计算,又更新进度条,如下所示:

def calc(w1, w2):
    update_progress_bar()
    return distance.levenshtein(w1,w2)
然后列表将变成:

 -1*np.array([[calc(w1,w2) for w1 in words] for w2 in tqdm(words)])

在我看来,列表理解应该只创建值,而不会造成任何副作用,因此,尽管简短且可以说是优雅的,但我还是会采用循环中的循环。在这种情况下,软件工程更加友好。

你很接近了。您现在的实现是错误的,因为您的条长度不是
len(words)
,而是
len(words)**2
,您应该更新每个操作。此外,当需要二维阵列时,您正在尝试构建一维阵列。那么,试试这个:

npa = []
akt = 1
num_words = len(words)
with progressbar.ProgressBar(max_value = num_words ** 2) as bar:
    for w2 in words:
        for w1 in words:
            npa.append(distance.levenshtein(w1,w2))
            bar.update(akt)
            akt = akt + 1
lev_similarity = -1*np.array(npa).reshape(num_words, num_words)

这样,您将在每次计算距离时更新栏。

我使用自己的小函数来完成此操作。我认为这对你的情况也可能有效

def print_pbar(n,m,s='|#.|',size=30,message=''):
    '''(int,int,string,int) => None
    Print a progress bar using the simbols in 's'.
    Example:
        range_limit = 1000
        for n in range(range_limit):
            print_pbar(n+1,m=range_limit)
            time.sleep(0.1)
    '''
    #adjust to bar size
    if m != size:
        n =(n*size)/m
        m = size
    #calculate ticks
    _a = int(n)*s[1]+(int(m)-int(n))*s[2]
    _b = round(n/(int(m))*100,1)
    #adjust overflow
    if _b >= 100:
        _b = 100.0
    #to stdout    
    sys.stdout.write(f'\r{message}{s[0]}{_a}{s[3]} {_b}%     ')
    sys.stdout.flush()


npa = []
for n, w2 in enumerate(words):
    print_pbar(n+1, len(words))
    for w1 in words:
        npa.append(distance.levenshtein(w1,w2))

不相同:ValueError:应为2D数组,而应为1D数组:数组=[0-106….]。使用数组重塑数据。如果数据具有单个功能或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。@Alex抱歉,我以为您唯一的问题是进度条,而不是
值错误
。我已经更新了我的答案,以反映这些问题。现在,我得到了
AttributeError:'list'对象没有属性“重塑”
,指向
lev\u similarity=-1*np.array(npa.reformate(num\u words,num\u words))