按步进值更改数组中的组号 [英] group numbers in an array by step value changes

查看:84
本文介绍了按步进值更改数组中的组号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像[101、107、106、199、204、205、207、306、310、312、312、314、317、318、380、377、379、382、466、469、469、471等的数组,472,557,559,562,566,569 ...]



在此数组中,经过几个整数后,值将发生阶跃变化。(就像在[101,107,106]和[199,204,...]之间)
或换一种说法,该数组由整数组组成,每组的值均以未知均值为中心。
但是我不知道总共有多少个组,并且每个整数的数量不确定。



我如何将这些整数归为一组



谢谢

解决方案

尝试以下操作:确定每对连续数字的差,然后从中确定平均差。

  nums = [101, 107、106、199、204、205、207、306、310、312、312、314、317、318,
380、377、379、382、466、469、471、472、557、559, 562、566、569]
对= list(zip(nums,nums [1:]))
diffs = [x,y成对的abs(xy)对]
avg_diff =和(diffs)/ len(diffs)#〜18.31

现在,您可以根据是否与上一个数字的差值低于或高于该平均值:

  groups = [[nums [0]]]#第一组已经有(x,y),zip中的d的第一个数字
iffs):如果d<
avg_diff:
个组[-1] .append(y)#添加到最后一个组
另一个:
个groups.append([y])#开始新组

或者如果您希望跨越三行的单线,则可能适合您:

  groups = [functools.reduce(lambda A,b:A +(b [1],)如果A else b,group,None)
作为密钥, itertools.groupby(zip(nums,nums [1:]),
key = lambda t:abs(t [0] -t [1])< 18.3)中的组,如果key]

您的示例结果如下:

  [[101,107,106],
[199,204,205,207],
[306,310,312,312,314,317,318],
[380,377,379,382],
[466,469,471,472],
[557,559,562,566,569]]

当然,如果某些组的组内部差异很大,例如 [1, 4,2,5,1042,1230,920,3,2,5] 。如果是这种情况,您可以尝试使用相对的数字差,例如 max(x,y)/ min(x,y)而不是 abs(xy)


i have an array like [101, 107, 106, 199, 204, 205, 207, 306, 310, 312, 312, 314, 317, 318, 380, 377, 379, 382, 466, 469, 471, 472, 557, 559, 562, 566, 569...]

In this array, after a few integers, there will be a step change in value.(like between [101,107,106] and [199,204,...]) Or put it in another way, the array is made up of groups of integers, each group with values centered around an unknown mean. But i won't know how many groups are there in total ,and the number of integers in each is uncertain.

How could i group those integers in each step change into different arrays.

Thanks

解决方案

You could try this: Determine the difference for each pair of consecutive numbers, and from those determine the average difference.

nums = [101, 107, 106, 199, 204, 205, 207, 306, 310, 312, 312, 314, 317, 318, 
        380, 377, 379, 382, 466, 469, 471, 472, 557, 559, 562, 566, 569]
pairs = list(zip(nums, nums[1:]))
diffs = [abs(x-y) for x, y in pairs]
avg_diff = sum(diffs) / len(diffs)  # ~ 18.31

Now, you can group the numbers by whether the difference to the previous number is lower or higher than that average:

groups = [[nums[0]]]          # first group already has first number
for (x, y), d in zip(pairs, diffs):
    if d < avg_diff:
        groups[-1].append(y)  # add to last group
    else:
        groups.append([y])    # start new group

Or if you prefer one-liners spanning three lines, this one might be for you:

groups = [functools.reduce(lambda A, b: A+(b[1],) if A else b, group, None) 
          for key, group in itertools.groupby(zip(nums, nums[1:]), 
                  key=lambda t: abs(t[0]-t[1]) < 18.3) if key]

Result for your example is this:

[[101, 107, 106],
 [199, 204, 205, 207],
 [306, 310, 312, 312, 314, 317, 318],
 [380, 377, 379, 382],
 [466, 469, 471, 472],
 [557, 559, 562, 566, 569]]

Of course, this breaks down if there are groups with vastly different group-internal differences, as in [1, 4, 2, 5, 1042, 1230, 920, 3, 2, 5]. If that is the case, you might try the relative difference of numbers instead, e.g. max(x,y)/min(x,y) instead of abs(x-y).

这篇关于按步进值更改数组中的组号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆