简体   繁体   English

变量+ =%可能或如何获得另一个数字之后的数字的下一个间隔

[英]variable +=% possible or how to get the next interval of a number after another number

I want to be able to do something like the following... 我希望能够做类似以下的事情......

Say I want a list of every number from 100-500 that is a multiple of 33. 假设我想要一个100-500的每个数字的列表,它是33的倍数。

>>> a = 100
>>> b = 500
>>> range(a,b,33)
[100, 133, 166, 199, 232, 265, 298, 331, 364, 397, 430, 463, 496]

This is not what I want, this is because a is not a multiple of 33. 这不是我想要的,这是因为a不是33的倍数。

To get the next multiple of 33 from a I can do: 从获得的33的倍数a我可以这样做:

a = a - a % 33 + 33

I want to know if there is an easier way to do this so that if I want to create this range without knowing the actual values and without having to define them beforehand.. 我想知道是否有更简单的方法来做到这一点,如果我想在不知道实际值的情况下创建这个范围而不必事先定义它们。

Such as: 如:

>>> def multiple(a, b, c):
    return range(a+=%c, b, c) #if this was possible

And obviously it would return me a range which would be correct, for example: 显然它会返回一个正确的范围,例如:

>>> multiple(100, 500, 33)
[132, 165, 198, 231, 264, 297, 330, 363, 396, 429, 462, 495]

I am aware I can do something simple like: 我知道我可以做一些简单的事情:

range(a - a%c + c, b, c)

However, without getting into details, calling the value of a is expensive for me in my case, and I would like to be able to find a way to not have to call it a second time, and also the above method is really not nice looking at all. 然而,如果没有进入细节,称值a对我来说是昂贵的在我的情况,我希望能够找到一种方法,没有把它称为第二次,而且上面的方法真的不是很好看着所有人。

I really was not sure what the title of my question should be, but I suppose what I am looking for is a way to find the next multiple of a number after another given number. 我真的不确定我的问题的标题应该是什么,但我想我正在寻找的是一种方法来找到另一个给定数字之后的数字的下一个数字。

Thank you. 谢谢。

The following does what you want and only uses a once: 下面,你想要做什么,只用a一次:

In [4]: range((a + c - 1) // c * c, b + 1, c)
Out[4]: [132, 165, 198, 231, 264, 297, 330, 363, 396, 429, 462, 495]

I would still place it into a helper function, rendering moot the question of how many times a is used. 我还是把它放到一个辅助功能,使实际意义的多少倍的问题a被使用。

Unlike the code in your question, this actually works correctly for cases when a is evenly divisible by c . 与您问题中的代码不同,这实际上适用于a可被c整除的情况。 Also, unlike the code in your question, it includes b in the range (as it should, according to your answer to my question in the comments): 此外,与您问题中的代码不同,它包含范围内的b (根据您在评论中对我的问题的回答,应该如此):

In [15]: range((a + c - 1) // c * c, b + 1, c)
Out[15]: [66, 99, 132]

Well, to get the multiple of 33 everytime, you would need to start with a multiple of 33 only. 好吧,要获得每次33的倍数,你需要从multiple of 33multiple of 33开始。

So, start with first multiple of 33 after 100 , because range function will just keep on adding 33 to get next multiples. 所以,从100之后的第一个33开始,因为range函数将继续添加33以获得下一个倍数。

To get the first multiple of 33 after a number num , you can use: - 要获得数字num33的第一个倍数,您可以使用: -

num / 33 * 33 + 33  <==>  (num / 33 + 1) * 33

So, for your range you can use: - 因此,对于您的范围,您可以使用: -

>>> a = 100
>>> b = 500
>>> a = (a / 33 + 1) * 33
>>> range(a, b, 33)
[132, 165, 198, 231, 264, 297, 330, 363, 396, 429, 462, 495]

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

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