我有一个变量,x我想知道它是否指向一个函数.
我希望我可以这样做:
>>> isinstance(x, function)
Run Code Online (Sandbox Code Playgroud)
但这给了我:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'function' is not defined
Run Code Online (Sandbox Code Playgroud)
我选择它的原因是因为
>>> type(x)
<type 'function'>
Run Code Online (Sandbox Code Playgroud) 我有一个unicode对象元组列表:
>>> t = [('?',), ('?',)]
Run Code Online (Sandbox Code Playgroud)
打印出来,我得到:
>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
Run Code Online (Sandbox Code Playgroud)
我想这是这些字符串的utf-8字节码表示的列表?
但我想看到的是打印出来的,惊喜:
[('?',), ('?',)]
Run Code Online (Sandbox Code Playgroud)
但是我在将字节码重新变成人类可读的形式方面遇到了过多的麻烦.
我有一个相当大的字符串(~700k),我需要运行10个正则表达式并计算任何正则表达式的所有匹配项.我的快速和肮脏的impl是做一些像re.search('(expr1)|(expr2)| ...'),但我想知道我们是否通过循环匹配看到任何性能提升:
换句话说,我想比较以下的表现:
def CountMatchesInBigstring(bigstring, my_regexes):
"""Counts how many of the expressions in my_regexes match bigstring."""
count = 0
combined_expr = '|'.join(['(%s)' % r for r in my_regexes])
matches = re.search(combined_expr, bigstring)
if matches:
count += NumMatches(matches)
return count
Run Code Online (Sandbox Code Playgroud)
VS
def CountMatchesInBigstring(bigstring, my_regexes):
"""Counts how many of the expressions in my_regexes match bigstring."""
count = 0
for reg in my_regexes:
matches = re.search(reg, bigstring)
if matches:
count += NumMatches(matches)
return count
Run Code Online (Sandbox Code Playgroud)
我将不再懒惰并明天进行一些测试(并发布结果),但我想知道答案是否会跳到真正了解正则表达式如何工作的人身上:)
我已经计算了一组值的均值和方差,并且我希望传递表示集合中每个数字的std偏差#的值.有没有更好的术语,或者我应该称之为num_of_std_devs_from_mean ...