python list columns split

时间:2016-09-10 12:18:38

标签: python list listview interpolation

有人可以帮我吗?这是一个复杂的问题,但我会尝试解释它。我需要为所有数据插入两个值Depths(Z)到Speed(v)

x= [55,55,55,,44,44,44,,33,33,33,] (coordinates)
z =[10,5,0,10,7,0,10,9,0]  (depths)
v= [20,21,22=,23,24,25,26,27,28] (speed)

结果

DEPTHS   SPEED(55)        SPEED(44)              SPEED(33)     
10       20              23                      26
6        21.5(interp)    24.5(interp)            27.5 (interp)
0        22              25                      28

我做了什么:

import numpy as np
    X_list=[55,55,55,44,44,44,33,33,33] 
    Z_list=[10,5,0,10,7,0 10,9,0]
    V_list=[20,21,22,23,24,25,26,27,28]
    x = np.linspace(min(Z_list),max(Z_list),num = 3) #(Find min and max values in  all list, and put step
    d=np.interp(x, Z_list, V_list) # interpolation for interesting depths
    zipped = list(zip(x,d))
    print (*zipped, sep="\n")

实际上我从第一个cordinate获得了信息

 DEPTHS    SPEED(55)    SPEED (44)      SPEED(33)    

    (10     20)          ?             ?
    (6      21.5)        ?             ?
    (0      22)          ?             ?

但我不知道如何从另一个坐下获得其他价值观。 我不知道如何将链接坐标与速度和深度联系起来并将其放到列中。

1 个答案:

答案 0 :(得分:1)

一种可能性是创建一个字典,将每个X坐标映射到具有该X坐标的元组列表:

>>> tupus = [ (1,0,1), (1,13,4), (2,11,2), (2,14,5) ]
>>> from collections import defaultdict
>>> tupudict = defaultdict( lambda: [] ) # default val for a key is empty list
>>> for tupu in tupus: tupudict[tupu[0]].append(tupu)
...
>>> tupudict[1]
[(1, 0, 1), (1, 13, 4)]
>>> tupudict[2]
[(2, 11, 2), (2, 14, 5)]

然后按键处理dict键,或将值转储到元组列表或其他任何列表中。

编辑为您的评论添加关于仅拆分列表的答案:

>>> from collections import defaultdict
>>> mylist = [11,11,11,11,12,12,15,15,15,15,15,15,20,20,20]
>>> uniquedict = defaultdict( lambda: [] )
>>> for n in mylist: uniquedict[n].append(n)
...
>>> uniquedict
defaultdict(<function <lambda> at 0x00000000033092E8>, {20: [20, 20, 20], 11: [11, 11, 11, 11], 12: [12, 12], 15: [15, 15, 15, 15, 15, 15]})
>>> uniquedict[11]
[11, 11, 11, 11]

请注意,对于输入列表中的每个n,uniquedict [n]现在是输入列表中所有n的列表。