简体   繁体   English

Pythonic编写一个不使用循环索引的for循环的方法

[英]Pythonic way to write a for loop that doesn't use the loop index

This is to do with the following code, which uses a for loop to generate a series of random offsets for use elsewhere in the program. 这与下面的代码有关,它使用for循环生成一系列随机偏移量,以便在程序的其他地方使用。

The index of this for loop is unused, and this is resulting in the 'offending' code being highlighted as a warning by Eclipse / PyDev 此for循环的索引未使用,这导致'违规'代码被Eclipse / PyDev强调为警告

def RandomSample(count):    
    pattern = []
    for i in range(count):
        pattern.append( (random() - 0.5, random() - 0.5) )

    return pattern

So I either need a better way to write this loop that doesn't need a loop index, or a way to tell PyDev to ignore this particular instance of an unused variable. 所以我要么需要一种更好的方法来编写不需要循环索引的循环,或者告诉PyDev忽略未使用变量的这个特定实例的方法。

Does anyone have any suggestions? 有没有人有什么建议?

Just for reference for ignoring variables in PyDev 仅供参考忽略PyDev中的变量

By default pydev will ignore following variables 默认情况下,pydev将忽略以下变量

['_', 'empty', 'unused', 'dummy']

You can add more by passing supression parameters 您可以通过传递抑制参数来添加更多内容

-E, --unusednames  ignore unused locals/arguments if name is one of these values

Ref: http://eclipse-pydev.sourcearchive.com/documentation/1.0.3/PyCheckerLauncher_8java-source.html 参考: http//eclipse-pydev.sourcearchive.com/documentation/1.0.3/PyCheckerLauncher_8java-source.html

How about itertools.repeat : itertools.repeat怎么样:

import itertools
count = 5
def make_pat():
    return (random() - 0.5, random() - 0.5)
list(x() for x in itertools.repeat(make_pat, count))

Sample output: 样本输出:

[(-0.056940506273799985, 0.27886450895662607), 
(-0.48772848046066863, 0.24359038079935535), 
(0.1523758626306998, 0.34423337290256517), 
(-0.018504578280469697, 0.33002406492294756), 
(0.052096928160727196, -0.49089780124549254)]
randomSample = [(random() - 0.5, random() - 0.5) for _ in range(count)]

Sample output, for count=10 and assuming that you mean the Standard Library random() function: 示例输出,对于count=10并假设您的意思是标准库random()函数:

[(-0.07, -0.40), (0.39, 0.18), (0.13, 0.29), (-0.11, -0.15),\
(-0.49, 0.42), (-0.20, 0.21), (-0.44, 0.36), (0.22, -0.08),\
(0.21, 0.31), (0.33, 0.02)]

If you really need to make it a function, then you can abbreviate by using a lambda : 如果你真的需要使它成为一个函数,那么你可以使用lambda缩写:

f = lambda count: [(random() - 0.5, random() - 0.5) for _ in range(count)]

This way you can call it like: 这样你就可以这样称呼它:

>>> f(1)
f(1)
[(0.03, -0.09)]
>>> f(2)
f(2)
[(-0.13, 0.38), (0.10, -0.04)]
>>> f(5)
f(5)
[(-0.38, -0.14), (0.31, -0.16), (-0.34, -0.46), (-0.45, 0.28), (-0.01, -0.18)]
>>> f(10)
f(10)
[(0.01, -0.24), (0.39, -0.11), (-0.06, 0.09), (0.42, -0.26), (0.24, -0.44) , (-0.29, -0.30), (-0.27, 0.45), (0.10, -0.41), (0.36, -0.07), (0.00, -0.42)]
>>> 

you get the idea... 你明白了......

Late to the party, but here's a potential idea: 晚会,但这是一个潜在的想法:

def RandomSample(count):
    f = lambda: random() - 0.5
    r = range if count < 100 else xrange # or some other number
    return [(f(), f()) for _ in r(count)]

Strictly speaking, this is more or less the same as the other answers, but it does two things that look kind of nice to me. 严格地说,这与其他答案或多或少相同,但它确实对我有两件好事。

First, it removes that duplicate code you have from writing random() - 0.5 twice by putting that into a lambda. 首先,它通过将random() - 0.5放入lambda来删除你写的random() - 0.5两次的重复代码。

Second, for a certain size range, it chooses to use xrange() instead of range() so as not to unnecessarily generate a giant list of numbers you're going to throw away. 其次,对于一定的大小范围,它选择使用xrange()而不是range()以免不必要地生成一个你要丢弃的巨大数字列表。 You may want to adjust the exact number, because I haven't played with it at all, I just thought it might be a potential efficiency concern. 您可能想要调整确切的数字,因为我根本没有使用它,我只是认为它可能是一个潜在的效率问题。

There should be a way to suppress code analysis errors in PyDev, like this: 应该有一种方法来抑制PyDev中的代码分析错误,如下所示:

http://pydev.org/manual_adv_assistants.html http://pydev.org/manual_adv_assistants.html

Also, PyDev will ignore unused variables that begin with an underscore, as shown here: 此外,PyDev将忽略以下划线开头的未使用变量,如下所示:

http://pydev.org/manual_adv_code_analysis.html http://pydev.org/manual_adv_code_analysis.html

Try this: 试试这个:

while count > 0:
    pattern.append((random() - 0.5, random() - 0.5))
    count -= 1
import itertools, random   

def RandomSample2D(npoints, get_random=lambda: random.uniform(-.5, .5)):
    return ((r(), r()) for r in itertools.repeat(get_random, npoints))
  • uses random.uniform() explicitly 显式使用random.uniform()
  • returns an iterator instead of list 返回迭代器而不是列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM