Possible Duplicate: Only index needed: enumerate or (x)range? Which of these would be considered better/clearer/faster/more 'Pythonic'? I don't care about the content of the list L , just how long it is. a = [f(n) for n, _ in enumerate(L)] or a = [f(n) for n in range(len(L))] If it makes any difference, the function f makes use of len(list) as well. Some quick timing runs s
可能重复: 只需要索引:枚举或(x)范围? 哪一个会被认为是更好/更清晰/更快/更'Pythonic'? 我不关心列表L的内容,只是它有多长。 a = [f(n) for n, _ in enumerate(L)] 要么 a = [f(n) for n in range(len(L))] 如果它有什么区别,函数f也使用len(list) 。 一些快速计时运行似乎给第二个选项使用range()稍微优于enumerate() : timeit a = [f(n) for n, _ in enumerate(mlist)] 10000 loops, best of 3:
What is faster, a for loop using enumerate or using xrange? EDIT: I have tested, and I just see minimal differences. Enumerate is slightly faster. Tested in Python 3: >>>import pygame >>>pygame.init() >>>clock = pygame.time.Clock() >>>a = list(range(100000)) >>>def do_with_range(): ... clock.tick() ... k = 0 ... for i in range(len(a)): .
什么是更快,循环使用枚举或使用xrange? 编辑:我测试了,我只看到最小的差异。 枚举速度稍快。 在Python 3中测试: >>>import pygame >>>pygame.init() >>>clock = pygame.time.Clock() >>>a = list(range(100000)) >>>def do_with_range(): ... clock.tick() ... k = 0 ... for i in range(len(a)): ... k += a[i] ... print(clock.tick()) >>&g
显然xrange更快,但我不知道为什么它更快(除了迄今为止的轶事更快),或者除此之外还有什么不同for i in range(0, 20): for i in xrange(0, 20): range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements. xrange is a sequence object that evaluates lazily. It should be added from @Thiago's hint, that in python3, range does the equivalent of python's
显然xrange更快,但我不知道为什么它更快(除了迄今为止的轶事更快),或者除此之外还有什么不同for i in range(0, 20): for i in xrange(0, 20): 范围会创建一个列表,所以如果您使用range(1, 10000000)它会在内存中创建一个包含9999999元素的列表。 xrange是一个懒惰评估的序列对象。 它应该从@ Thiago的提示中加入,在python3中,范围相当于python的xrange 范围会创建一个列表,所以如果您使用range(1, 10000000)它会在
I set an environment variable that I want to access in my Python application. How do I get this value? Environment variables are accessed through os.environ import os print(os.environ['HOME']) Or you can see a list of all the environment variables using: os.environ As sometimes you might need to see a complete list! # using get will return `None` if a key is not present rather than raise
我设置了一个我想在我的Python应用程序中访问的环境变量。 我如何得到这个值? 环境变量可以通过os.environ访问 import os print(os.environ['HOME']) 或者您可以使用以下方式查看所有环境变量的列表: os.environ 有时您可能需要查看完整列表! # using get will return `None` if a key is not present rather than raise a `KeyError` print(os.environ.get('KEY_THAT_MIGHT_EXIST')) # os.getenv is equivalent, and
Moving to python with C/Java background, I recently had to implement a mutual recursion, but something in python is bothering me: since a python program is interpreted line by line, if I have two functions one after another in the same python file: def A(n): B(n-1) # if I add A(1) here, it gives me an error def B(n): if n <= 0: return else: A(n-1) When the inter
用C / Java背景移植到python,我最近不得不实现一个相互递归,但是Python中的某些东西让我感到困扰: 因为一个python程序是逐行解释的,如果我在同一个python文件中有两个函数: def A(n): B(n-1) # if I add A(1) here, it gives me an error def B(n): if n <= 0: return else: A(n-1) 当解释器读取A , B尚未定义,但此代码不会给我一个错误 TL; DR我的理解是,当解释def时,python用{&
I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is from the documentation: class C(object): def __init__(self): self._x = None def getx(self): return self._x def setx(sel
我想了解内置函数property如何工作。 令我困惑的是,该property也可以用作装饰器,但只有在作为内置函数使用时才需要参数,而不能在用作装饰器时使用参数。 这个例子来自文档: class C(object): def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, de
This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In this instance I define 'most pythonic' to mean that it follows the 'principle of least astonishment'. I have multiple classes whic
这个问题不是用来讨论单例设计模式是否合意,是反模式还是用于任何宗教战争,而是讨论如何在python中最好地实现这种模式。 在这种情况下,我将'最pythonic'定义为它遵循'最不惊讶的原则'。 我有多个类将成为单身(我的用例是一个记录器,但这不重要)。 当我可以简单地继承或装饰时,我不希望杂乱地加上几个带有gumph的类。 最佳方法: 方法1:装饰器 def singleton(class_): instances = {} de
I'm using virtualenv and the virtualenvwrapper. I can switch between virtualenv's just fine using the workon command. me@mymachine:~$ workon env1 (env1)me@mymachine:~$ workon env2 (env2)me@mymachine:~$ workon env1 (env1)me@mymachine:~$ However, how do I exit all virtual machines and workon my real machine again? Right now, the only way I have of getting back to me@mymachine:~$ is
我使用的是virtualenv和virtualenvwrapper。 我可以使用workon命令在virtualenv之间切换。 me@mymachine:~$ workon env1 (env1)me@mymachine:~$ workon env2 (env2)me@mymachine:~$ workon env1 (env1)me@mymachine:~$ 但是,如何退出所有虚拟机并再次运行我的真机? 现在,我必须回到过去的唯一途径 me@mymachine:~$ 是退出shell并开始一个新的。 这有点令人讨厌。 有没有一个命令“没有”,如果是的话,它是什么? 如
I'm a python beginner and I'm in my first days playing with my own python scripts and projects such as django. I use Ubuntu and I set my PYTHONPATH as export PYTHONPATH=/usr/lib/python2.6:~/Projects/mypython When I run into a python interpreter import sys print sys.path I can see ['', '/usr/bin', '/usr/local/lib/python2.6/dist-packages/ropemode-0.1_rc2-py2.6.e
我是一名python初学者,我在第一天玩我自己的python脚本和项目,比如django。 我使用Ubuntu,并将我的PYTHONPATH设置为 export PYTHONPATH=/usr/lib/python2.6:~/Projects/mypython 当我遇到一个Python解释器 import sys print sys.path 我可以看到 ['','/ usr / bin','/usr/local/lib/python2.6/dist-packages/ropemode-0.1_rc2-py2.6.egg','/usr/local/lib/python2.6 /dist-packages/
I have a function that, in the simplest of cases, operates on an iterable of items. def foo(items): for item in items: # do stuff Sometimes, I do not want to pass it an iterable of items directly, but rather an object that provides a method to get the iterable: def foo(obj): for item in obj.iteritems(): # do same stuff as above I can merge these two cases like this: d
我有一个函数,在最简单的情况下,它可以在一系列迭代项上运行。 def foo(items): for item in items: # do stuff 有时候,我不想直接传递一个迭代项,而是提供一种获得迭代的方法的对象: def foo(obj): for item in obj.iteritems(): # do same stuff as above 我可以像这样合并这两种情况: def foo(obj): try: items = obj.iteritems() except ValueError: items = obj