简体   繁体   English

使用 Python 在特定数字范围内向上/向下计数

[英]Count up/down within a specific number range using Python

I'm sure this must've been asked before, but I couldn't find anything like that, sorry.我敢肯定以前肯定有人问过这个问题,但我找不到类似的东西,抱歉。

Let's say I have a list of numbers [1..24] :假设我有一个数字列表[1..24]

numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

I want to make a function that accepts two integers: mynum which is a number in the list and modifier .我想创建一个接受两个整数的函数: mynum是列表中的一个数字和modifier It will then find mynum in the list and then cycle through the list modifier number of positions (right if positive, left if negative).然后mynum在列表中找到mynum ,然后循环遍历列表modifier的位置数(如果为正则为右,如果为负则为左)。 If it reaches the end of the list, it will loop to the other side.如果它到达列表的末尾,它将循环到另一侧。

What I want now is to basically shift the number mynum based on the input, within the list.我现在想要的是基本上根据输入在列表中移动数字mynum

Example:例子:

shift_function(2, 1)   # returns 3
shift_function(23, 3)  # returns 2
shift_function(4, -8)  # returns 20
shift_function(15, 51) # returns 18

How can I cycle through the ends of a list like this?我怎样才能循环遍历这样的列表的末尾?

I thought of using the time module to get a time with mynum as current hour and then add/subtract modifier hours to it.我想使用time模块将mynum作为当前小时获取时间,然后添加/减去modifier小时数。 This will work with my [1..24] range but I'm looking for a more general solution to this what seems to be a simple problem.这将适用于我的[1..24]范围,但我正在寻找一个更通用的解决方案,这似乎是一个简单的问题。

Edit: To clearify further, I simply want to count through a list of numbers.编辑:为了进一步澄清,我只想通过一个数字列表进行计数。 If the end is reached, start over from the beginning, same backwards.如果到达终点,则从头开始,同样向后。

Edit2: I don't know how much more clear I have to be. Edit2:我不知道我必须清楚多少。 Imagine you have big numbers on your desk: 3,4,5,6,7.想象一下,你的桌子上有很大的数字:3、4、5、6、7。 And then you start at the 5 and take modifier steps to the right, let's say 3 steps.然后从第 5 步开始向右进行modifier步骤,假设为 3 个步骤。 So you count: "six, seven, three", starting over from the beginning again.所以你数:“六、七、三”,又从头开始。 If modifier is negative, you count backwards.如果modifier为负数,则向后计数。 Eg with -4 you count: "four, three, seven, six".例如,用-4计算:“四、三、七、六”。

I can't see how this is so hard to get.我不明白这怎么这么难得到。 Seems like a very simple and common problem to me.对我来说似乎是一个非常简单和常见的问题。

Looks like this function does what you want:看起来这个函数做了你想要的:

def shift_function(n, shift):
   return numbers[(numbers.index(n) + shift) % len(numbers)]

You can do this with numpy , using np.roll .您可以使用numpy执行此操作,使用np.roll Just be aware that the negative is actually inverse relative to what you want, so you can use -modifier instead:请注意,负数实际上与您想要的相反,因此您可以使用-modifier代替:

import numpy as np

numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

mynum = 4
modifier = -8

>>> np.roll(numbers,-modifier)[numbers.index(mynum)]
20

mynum = 15
modifier = 51

>>> np.roll(numbers,-modifier)[numbers.index(mynum)]
18

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

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