2012-05-17 32 views
2

我希望有人可以帮我解决我被卡住(再次)的问题。Python - 对数组第2部分列表进行分类

如果我有坐标:

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]) 

最好的问候,

格伦

+3

你应该真的把这个问题全包。我不知道你在说什么,直到我读完你之前的问题。 – jdi

+0

@jdi,好的。下次我会更加小心。关于 – glwilliam

回答

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], ...] 
+0

我seeee!这就是它的工作原理!谢啦!也感谢您的解释 – glwilliam

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

我引用@jamylak的答案从your previous question和showi稍作修改。

虽然你可以尝试和符合您造成xy的模式,你也可以只修改原来的解决方案来治疗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))] 

你可以看到它可以是任意的数据集,并且它总是在每个数据集的第一个元素上分组。

+0

感谢您的回复。这是真的,结果是不可取的。因为我希望'x'和'y'是分开的,它们的格式与上面相同。 (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)] 因为稍后,我会输入他们在我的拉格朗日公式。但我现在试着将它分开。 – glwilliam

+0

@glwilliaz:更新了如何将它们分开 – jdi

+1

+1我喜欢这个解决方案,因为它使用每个连续点的0th元素,它支持任意数量的数组,例如。这也适用于'z'点的列表。我建议用这个替换你的最后一个班轮:'x,y = zip(* [zip(* points)for points in groups(zip(x,y))])' – jamylak

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 

相关问题