小编Dan*_*l H的帖子

如何检测Python变量是否为函数?

我有一个变量,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)

python

629
推荐指数
15
解决办法
23万
查看次数

如何以原始语言打印unicode字符串的元组(不是u'foo'形式)

我有一个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)

但是我在将字节码重新变成人类可读的形式方面遇到了过多的麻烦.

python unicode

12
推荐指数
1
解决办法
5821
查看次数

正则表达式'|' 运算符vs每个子表达式的单独运行

我有一个相当大的字符串(~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)

我将不再懒惰并明天进行一些测试(并发布结果),但我想知道答案是否会跳到真正了解正则表达式如何工作的人身上:)

python regex performance

5
推荐指数
3
解决办法
2971
查看次数

什么是远离平均值的"std偏差数"的正确术语

我已经计算了一组值的均值和方差,并且我希望传递表示集合中每个数字的std偏差#的值.有没有更好的术语,或者我应该称之为num_of_std_devs_from_mean ...

statistics

3
推荐指数
1
解决办法
682
查看次数

标签 统计

python ×3

performance ×1

regex ×1

statistics ×1

unicode ×1