分类数组在Python列表 [英] Categorizing the list of array in python
问题描述
我帮助我的朋友做在python逻辑算法,但我没有配备最好的解决办法呢。
首先,我有数组列表:
X =数组[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
和他要分类的X所以输出变成了这个样子:
阵列([0,1,2,3,4])#增加值
阵列([4,3,2])#decreasing值
阵列([2,3])#增加值
阵列([3,-2,-4,-7])#decreasing值
阵列([ - 7,2])#增加值
阵列([2,2])#remain_the_same_value
的规则很简单:
- 如果值不断增加(如上面的例子:0,1,2,3,4),他们把在一个阵列中
- 如果值保持下降(如上面的例子:3,-2,-4,-7)他们把在一个阵列中
- 但是,如果在值图案的突然变化,如上面的例子:从增加值(0,1,2,3,4)突然的下一个值减少。新阵列将作出,并把最后的价值越来越,其为(4),并且监视下一个值,无论是减少值或没有。如果是的话,它们将被放置在一个阵列。例如:阵列([4,3,2])
- ,如果该值保持(上述像例如,从2到2)相同。它们将被放置在一个阵列中。
块引用>这是我走了这么远,但仍远解决方案
#categorize哪些类型的输入
如果len(X)> 2:
因为我在范围内(LEN(X)):
如果(X [I + 1] -x [I])GT; 0和i + I< LEN(X):#增加x值 ELIF(X [I + 1] -x [I])LT; 0和i + I< LEN(X):#用于减少的x值 ELIF(X [I + 1] -x [I])== 0和i + I< LEN(X):#为车辆的向前的方向 其他:
打印'错误:检查输入坐标再一次!最好的问候,
格伦
解决方案首先,我想说,我不明白你的问题的一部分,
阵列([3,-2])#decreasing值
阵列([ - 2,-4,-7])#decreasing值为什么这些单独的?
我会后我的答案至今这使得除了那部分,因为我没有看到它背后的逻辑正确的结果。本例使用列表和元组为简单起见,但你可以改变它,如果你想使用数组。
>>>从进口和itertools GROUPBY
>>>数据= [0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
>>> DEF斜率(A,B):#Returns 1 INC,0平等和-1十二月
返回(一个或GT; B) - (A&下; B)>>> DEF组(NUMS):
对于K,V在GROUPBY(ZIP(NUMS,NUMS [1:]),拉姆达(X,Y):斜率(X,Y)):
收率下(ⅴ)+元组(y代表的x,y在ⅴ)#使用itertools.chain这可以写为元组(链(下一个(V),(Y为X,Y中V)))
>>>列表(组(数据))
[(0,1,2,3,4),(4,3,2),(2,3),(3,-2,-4,-7),(-7,2),(2, 2)]i was helping my friend to do logic algorithm in python but i haven't come with best solution yet.
first of all, i have a list of array:
x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
and he wanted to categorize the x so the output become like this:
array([0,1,2,3,4]) # increasing value array([4,3,2]) #decreasing value array([2,3]) # increasing value array([3,-2,-4,-7]) #decreasing value array([-7,2]) # increasing value array([2,2]) # remain_the_same_value
the rule is simple:
- if the value keep increasing (like example above: 0,1,2,3,4) they put in one array
- if the value keep decreasing (like example above: 3,-2,-4,-7) they put in one array
- but, if there is a sudden change in value pattern such as example above: from the increasing value (0,1,2,3,4) suddenly the next value is decreasing. the new array will be made and put the last increasing value which is (4) and monitor the next value, whether it is decreasing value or not. If yes, they will be put in one array. example :array([4,3,2])
- if the the value is remain the same (like example above, from 2 to 2). they will be put in one array.
this is what i come so far, but still far from the solution
#categorize which types of input if len(x) > 2 : for i in range(len(x)) : if (x[i+1]-x[i]) > 0 and i+i < len(x) : # for increasing x value elif (x[i+1]-x[i]) < 0 and i+i < len(x) : # for decreasing x value elif (x[i+1]-x[i]) == 0 and i+i < len(x) : # for foward direction of vehicle else : print 'ERROR : check the input coordinates once again!'
best regards,
Glenn
解决方案Firstly I would like to say that I don't understand one part of your question,
array([3,-2]) #decreasing value array([-2,-4,-7]) #decreasing value
Why are these separate?
I will post my answer so far which gives the correct results except for that section since i don't see the logic behind it. This example uses lists and tuples for simplicity but you can change it to use array if you want.
>>> from itertools import groupby >>> data = [0,1,2,3,4,3,2,3,-2,-4,-7,2,2] >>> def slope(a,b): #Returns 1 for inc, 0 for equal and -1 for dec return (a > b) - (a < b) >>> def groups(nums): for k,v in groupby(zip(nums,nums[1:]), lambda (x,y): slope(x,y)): yield next(v) + tuple(y for x,y in v) #Using itertools.chain this can be written as tuple(chain(next(v),(y for x,y in v))) >>> list(groups(data)) [(0, 1, 2, 3, 4), (4, 3, 2), (2, 3), (3, -2, -4, -7), (-7, 2), (2, 2)]
这篇关于分类数组在Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!