小编rsm*_*rsm的帖子

为什么"any()"运行比使用循环慢?

我一直在一个管理大量单词列表的项目中工作,并通过大量测试来验证这些单词是否有效.有趣的是,每次我使用像itertools模块这样的"更快"的工具时,它们似乎都会变慢.

最后我决定问这个问题,因为我可能做错了.以下代码将尝试测试any()函数的性能与循环的使用.

#!/usr/bin/python3
#

import time
from unicodedata import normalize


file_path='./tests'


start=time.time()
with open(file_path, encoding='utf-8', mode='rt') as f:
    tests_list=f.read()
print('File reading done in {} seconds'.format(time.time() - start))

start=time.time()
tests_list=[line.strip() for line in normalize('NFC',tests_list).splitlines()]
print('String formalization, and list strip done in {} seconds'.format(time.time()-start))
print('{} strings'.format(len(tests_list)))


unallowed_combinations=['ab','ac','ad','ae','af','ag','ah','ai','af','ax',
                        'ae','rt','rz','bt','du','iz','ip','uy','io','ik',
                        'il','iw','ww','wp']


def combination_is_valid(string):
    if any(combination in string for combination in unallowed_combinations):
        return False

    return True


def combination_is_valid2(string):
    for combination in unallowed_combinations:
        if combination in string:
            return False

    return True


print('Testing …
Run Code Online (Sandbox Code Playgroud)

python performance python-3.x

11
推荐指数
1
解决办法
248
查看次数

使用“尝试”来避免分段错误

最近在我的一个程序中,我遇到了分段错误问题。我设法找到引起问题的线,但没有找到解决它的方法。

该行:

self.window_player.add(self.vlc)
Run Code Online (Sandbox Code Playgroud)

在哪里self.vlc是一个小部件,self.window_playerGtk.Window()在林间空地中创建的空。

该行位于__init__我的程序的,因此实际上此问题仅在启动该程序时发生。奇怪的事实是,错误仅显示为(启动程序的)10次​​中的1次

错误:这 Segmentation fault是我从终端获得的唯一输出

所以我尝试了:

while True:
    try:
        self.window_player.add(self.vlc)
        break
    except:
        print "segmentation"
Run Code Online (Sandbox Code Playgroud)

问题是分割错误似乎并没有被try!排除。

python segmentation-fault pygobject gtk3

4
推荐指数
1
解决办法
2571
查看次数