在阵列中找到差异

发布于 2025-01-20 23:59:53 字数 518 浏览 1 评论 0原文

我在Python中有一个阵列,该阵列由电线之间的米(链条)距离(链条)组成。这是

d=([0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178])

我想编写一个脚本以计算每1000m或接近1000m但不超过1000m的差异(部分长度)的示例。

这是一个示例

  • pection1 = 997-0

  • pection2 = 1980-997

  • pection3 = 2178-1980

结果应该在阵列中,

Section = ([997, 983, 198])

我是Python的初学者。我还在学习。我没有附加任何代码,因为我对从哪里开始不了解。

I have an array in python which consists of distances in meters (chainage) between electric poles. Here’s a sample

d=([0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178])

I want to write a script to compute the difference (section length) at every 1000m or close to 1000m but not exceeding 1000m.

Here’s an example

  • Section1 = 997 - 0

  • Section2 = 1980 - 997

  • Section3 = 2178 - 1980

The result should be in array

Section = ([997, 983, 198])

I’m a beginner in python. I’m still learning. I have not attached any code because I do not have any idea on where to start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一绘本一梦想 2025-01-27 23:59:53

因此,这将稍久,因为您是一个初学者,我将尝试分解我在这里所做的事情,而不仅仅是粘贴代码。如果您有任何疑问,请随时回答和询问,但是无论如何,我们就可以了!因此,听起来您想开始使用您测量的所有值来创建一个列表。在Python中,您只需使用[](并且您将使用()创建一个元组)。因此,从您的列表开始...

d=[0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102,1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]

接下来,我们将需要找到差异。对于初学者来说,让我们创建一个变量,该变量将跟踪我们使用的最后一个值(将要减去),在这种情况下,该变量将以零为零。接下来,我们还将创建我们的第二个列表,我们将在其中存储差异。

current = 0
section = []

现在是通过它们循环的最棘手的部分。因此,在Python中,您可以遍历列表,但是要检查我们目前正在使用的项目,并确保它是最接近1000的项目,而无需越过,我们也需要访问下一个项目。因此,我们将通过一组数字进行迭代,而不是直接迭代列表,这些数字将是从第一个开始的列表中项目的地址。

for i in range(len(d) - 1):

此语句在这里说我们是从将I设置为零开始,并且将通过代码循环我们接下来输入,直到达到列表D负1的长度。因此,现在检查当前项目是否最接近1000不走过。

for i in range(len(d) - 1):
    if ((d[i] % 1000) == 0) or ((d[i] % 1000) > (d[i + 1] % 1000)):
        section.append(d[i] - current)
        current = d[i]

因此,现在本节说,如果D [i] mod 1000等于零(地址i的D项目是1000),或者如果正在研究的当前项目Mod 1000大于下一个(这表明下一个大于大于1000的倍数)然后,我们将一个元素添加到等于d [i]的差异列表中 - 然后将电流更改为等于d [i]。

这将负责大部分工作,现在我们只需要完成最后一行即可。

section.append(d[len(d) - 1] - current)

这条线采用了最后一个元素,并始终添加它和我们当前价值之间的区别,就像您似乎想要的那样。因此,最后我们的整个代码是...

d=[0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102,1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]
current = 0
section = []
for i in range(len(d) - 1):
    if ((d[i] % 1000) == 0) or ((d[i] % 1000) > (d[i + 1] % 1000)):
        section.append(d[i] - current)
        current = d[i]

section.append(d[len(d) - 1] - current)

希望这可以帮助您解决问题并学习一些东西。有一个很棒的!

So this will be slightly long since you're a beginner I'll try to break down some of what I'm doing here instead of just pasting code. If you have any questions feel free to respond and ask, but anyways here we go! So it sounds like you want to start out by creating a list with all of the values you've measured. In python to create a list you just use [] (and you'd use () to create a tuple). So starting with your list we have...

d=[0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102,1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]

Next up we are going to need to find the differences. For starters lets create a variable that will keep track of our last value used (what will be subtracting) which in this case will start out as zero. Next we will also create our second list where we will store the differences.

current = 0
section = []

Now is the trickiest part of looping through them. So in Python you can iterate through a list, but in order to check the item we're currently on and make sure it is the closest to 1000 without going over we need access to the next item too. So instead of directly iterating over the list, we're going to iterate through a set of numbers, and each of these numbers will be the address of an item on the list starting with the first.

for i in range(len(d) - 1):

this statement right here is saying we are starting by setting i to zero, and will go through the loop of code we type next until we reach the length of list d minus 1. So now to check if the current item is the closest to 1000 without going over.

for i in range(len(d) - 1):
    if ((d[i] % 1000) == 0) or ((d[i] % 1000) > (d[i + 1] % 1000)):
        section.append(d[i] - current)
        current = d[i]

So now this section is saying that if d[i] mod 1000 equals zero (the item in d at address i is 1000) or if the current item being investigated mod 1000 is greater than the next (which would indicate the next is greater than a multiple of 1000) then we add an element to our list of differences equal to d[i] - current and then change current to be equal to d[i].

This will take care of most of the work and now we just need to finish up with one last line.

section.append(d[len(d) - 1] - current)

This line takes the last element and always adds the difference between that and our current value like you seem to have wanted. So in the end our whole code is...

d=[0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102,1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]
current = 0
section = []
for i in range(len(d) - 1):
    if ((d[i] % 1000) == 0) or ((d[i] % 1000) > (d[i + 1] % 1000)):
        section.append(d[i] - current)
        current = d[i]

section.append(d[len(d) - 1] - current)

Hopefully this helped you solve your problem and learn a few things. Have a great one!

一口甜 2025-01-27 23:59:53

既然您需要一个 numpy 解决方案,那么让我们取消那些 for 循环:

d=np.array([0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178])

def func(d, k = 1000):
    insert = (np.arange(d.max() // k) + 1) * k                   #intermediate points
    vals = np.r_[d[0], d[np.searchsorted(d, insert) - 1], d[-1]] #values below the intermediate points
    return np.diff(vals)                                         #difference between the intermediate points

func(d)
Out[]: array([997, 983, 198])

Since you ask for a numpy solution, let's do away with those for loops:

d=np.array([0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178])

def func(d, k = 1000):
    insert = (np.arange(d.max() // k) + 1) * k                   #intermediate points
    vals = np.r_[d[0], d[np.searchsorted(d, insert) - 1], d[-1]] #values below the intermediate points
    return np.diff(vals)                                         #difference between the intermediate points

func(d)
Out[]: array([997, 983, 198])
尘世孤行 2025-01-27 23:59:53

即使你不知道如何有效地解决问题,尝试写一些东西,它可能只是如何获得 1000m 间隔,甚至是找到最接近值的繁琐脚本。这是学习如何编码的唯一方法,你必须练习,当你遇到困难时就去询问。从阅读别人的代码中获得的价值是有限的。

为了解决您的问题,我计算了与给定的特定列表相关的间隔。然后,我创建一个列表来存储列表中与间隔最接近的值,并使用 for 循环计算间隔。我使用列表理解来计算相关部分的差异。

    intervals = np.arange(min(d),max(d)+1000,1000)
    closest_values = [min(d)]
    for i in intervals:
        diff = abs((d-i)/i)
        closest_values.append(d[np.argmin(diff)])
    section = [closest_values[i+1] - closest_values[i] for i,_ in enumerate(closest_values[:-1]]

使用您的输入列表,将其转换为 numpy 数组后,给出了以下结果:

[997, 983, 198]

Even if you don't know how to solve the problem efficiently, try and write something, it can be just how to get the 1000m intervals, even a cumbersome script to find the closest values. This is the only way to learn how to code, you have to practice and when you get stuck go and ask. There is a limit for the amount of value you can get from reading someone else's code.

For the solution to your problem, I compute the intervals relevant for the specific list given. Then I create a list to store the closest values from your list to the intervals and compute the intervals using a for loop. I use a list comprehension to compute the relevant section differences.

    intervals = np.arange(min(d),max(d)+1000,1000)
    closest_values = [min(d)]
    for i in intervals:
        diff = abs((d-i)/i)
        closest_values.append(d[np.argmin(diff)])
    section = [closest_values[i+1] - closest_values[i] for i,_ in enumerate(closest_values[:-1]]

Using your input list, after converting it to a numpy array, gave me the result of:

[997, 983, 198]
残花月 2025-01-27 23:59:53

另一个选择:这可能是一个很好的机会,可以看一下 > groupby() 来自标准库的 模块。

尝试

from itertools import groupby

d = [0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]

for key, group in groupby(d, key=lambda n: n // 1000):
    print(f"{key = }, {list(group) = }")

,您将获得

key = 0, list(group) = [0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997]
key = 1, list(group) = [1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980]
key = 2, list(group) = [2083, 2178]

groupby chops d根据键>键函数的结果,这是1000 。

考虑到这一点,您可以尝试

result, last = [], 0
for _, group in groupby(d, key=lambda n: n // 1000):
    *_, current = group
    result.append(current - last)
    last = current

获得

[997, 983, 198]

Another option: This might be a good opportunity to take a look at groupby() from the standard library's itertools module.

Try

from itertools import groupby

d = [0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997, 1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980, 2083, 2178]

for key, group in groupby(d, key=lambda n: n // 1000):
    print(f"{key = }, {list(group) = }")

and you'll get

key = 0, list(group) = [0, 98, 198, 300, 400, 500, 600, 700, 800, 900, 997]
key = 1, list(group) = [1102, 1187, 1282, 1382, 1480, 1570, 1670, 1775, 1885, 1980]
key = 2, list(group) = [2083, 2178]

groupby chops d into blocks according to the results of the key-function, which is floor-division by 1000.

With that in mind you could try

result, last = [], 0
for _, group in groupby(d, key=lambda n: n // 1000):
    *_, current = group
    result.append(current - last)
    last = current

to get

[997, 983, 198]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文