Python - 对数组第2部分的列表进行分类

时间:2012-05-17 20:06:18

标签: python arrays logic

我希望有人可以帮助我解决我再次陷入困境的问题。

如果我有坐标:

 x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
 y = array[0,1,2,3,4,5,6,7,8,9,10,11,12]

Categorizing the list of array in python的帮助下,我可以:

x = [(0, 1, 2, 3, 4), (4, 3, 2), (2, 3), (3, -2, -4, -7), (-7, 2), (2, 2)]

问题是,我应该如何做到这样:

y = [(0,1,2,3,4),(4,5,6),(6,7),(7,8,9,10),(10,11),(11,12)]

因为,x和y实际上是坐标,它们相互联系。

我试过使用循环功能,我意识到代码仍然是错误的

se = []
for i in range(len(z)):
        k = z[i]
        for i in range(len(k)):
                se.append(y[i])

最好的问候,

格伦

4 个答案:

答案 0 :(得分:3)

我在your previous question引用了@jamylak的回答,并略有修改。

虽然您可以尝试将结果x的模式与y匹配,但您也可以修改原始解决方案,将x和y视为点(x,y):

from itertools import groupby

x = [0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
y = [0,1,2,3,4,5,6,7,8,9,10,11,12]

def slope(a,b): #Returns 1 for inc, 0 for equal and -1 for dec
    return (a > b) - (a < b) 

def groups(nums):
    # 
    # Change the call to slope() to assume 2d point tuples as values
    #
    for k,v in groupby(zip(nums,nums[1:]), lambda (x,y): slope(x[0],y[0])):
        yield next(v) + tuple(y for x,y in v) 

#
# Pass in a zipped data structure
#
print list(groups(zip(x,y)))
# result
[((0, 0), (1, 1), (2, 2), (3, 3), (4, 4)),
 ((4, 4), (3, 5), (2, 6)),
 ((2, 6), (3, 7)),
 ((3, 7), (-2, 8), (-4, 9), (-7, 10)),
 ((-7, 10), (2, 11)),
 ((2, 11), (2, 12))]

虽然我不确定是否需要生成的格式。

以下是将它们分开的方法:

from operator import itemgetter

result = list(groups(zip(x,y)))
x = [map(itemgetter(0), points) for points in result]
y = [map(itemgetter(1), points) for points in result]
print x
# [[0, 1, 2, 3, 4], [4, 3, 2], [2, 3], [3, -2, -4, -7], [-7, 2], [2, 2]]
print y
# [[0, 1, 2, 3, 4], [4, 5, 6], [6, 7], [7, 8, 9, 10], [10, 11], [11, 12]]

或者@jamylak建议:

x,y = zip(*[zip(*points) for points in result])

为了说明@jamylak正在谈论的内容,关于groups()方法的修改如何允许N维点或数据集:

z = ['foo',1,2,'bar',4,5,6,'foo',8,9,10,'bar',12]
print list(groups(zip(x,y,z)))
# result
[((0, 0, 'foo'), (1, 1, 1), (2, 2, 2), (3, 3, 'bar'), (4, 4, 4)),
 ((4, 4, 4), (3, 5, 5), (2, 6, 6)),
 ((2, 6, 6), (3, 7, 'foo')),
 ((3, 7, 'foo'), (-2, 8, 8), (-4, 9, 9), (-7, 10, 10)),
 ((-7, 10, 10), (2, 11, 'bar')),
 ((2, 11, 'bar'), (2, 12, 12))]

你可以看到它可以是任意数据集,它总是只对每个数据集的第一个元素进行分组。

答案 1 :(得分:2)

以下是您想要的:

x = [(0, 1, 2, 3, 4), (4, 3, 2), (2, 3), (3, -2, -4, -7), (-7, 2), (2, 2)]
y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

s = map(len, x)
s = [0] + [sum(s[:i])-i for i in range(1, len(s))] + [len(y)]
y = [tuple(y[a:b+1]) for a, b in zip(s, s[1:])]

结果:

>>> y
[(0, 1, 2, 3, 4), (4, 5, 6), (6, 7), (7, 8, 9, 10), (10, 11), (11, 12)]

这基本上构建了y将被拆分的位置列表。我们使用x中每个元组的长度来计算它,但它有点棘手,因为集合的最后一个元素被包含在下一组中的下一个元素中。

以下是其中一个中间值,可能有助于阐明其工作原理:

>>> zip(s, s[1:])
[(0, 4), (4, 6), (6, 7), (7, 10), (10, 11), (11, 13)]

我们使用它来构建新的y,如下所示:

[(0, 4), (4,  6), (6,  7), (7, 10), (10, 11), (11, 13)]
   \ |     \   \    \_  \
   | |      \  |      \  |
[y[0:4+1], y[4:6+1], y[6:7+1], ...]

答案 2 :(得分:0)

有点难看,但它有效:

se = []
y2 = [y[0]]
i = 1
for xrun in x:
    first = True
    for xv in xrun:
        if first:
            first = False
            continue
        y2.insert( len(y2), y[i] )
        i += 1
    se.insert( len(se), tuple(y2) )
    y2 = [y[i-1]]

答案 3 :(得分:0)

这个如何使用numpy,它同时解决了你的第一个问题。

import numpy as np

x=(0, 1, 2, 3, 4, 3, 2, 3, -2, -4, -7, 2, 2)
y=range(13)

#First order differential, find slopes
dx = list((np.diff(x)>0)*1)

#First order differental looses first value, but we always want to keep it
#just decide if it's lower or higher than the 2nd value

d0=((x[0]-x[1])>0)*1

#Add the first order differential to the 2nd order differential (peaks)
ddx = [d0,]+list(np.abs(np.diff(dx)))

p=0
rx=[]
ry=[]

for n,v in enumerate(ddx):
    if v==1:
        rx.append(tuple(x[p:n+1]))
        ry.append(tuple(y[p:n+1]))
        p=n

print rx
print ry