繁体   English   中英

FInd(X,Y)对和关联的Z坐标

[英]FInd (X, Y) pairs and associated Z coordinates

我有一个列表列表DataCoord

[nodeID, X, Y, Z]

多行。 一些节点将具有相同的XY坐标以及不同的Z坐标。 例如,说我有:

Pair (X,Y) = (0,0):
[1, 0, 0, 0,]: level 0
[m, 0, 0, 200]: level m
[n, 0, 0, 100]: level n #the DataCoord list may not be ordered

我想识别并排序此(x,y)对的不同z等级。
对于共享相同XY坐标的其他部分也是如此。

任何帮助表示赞赏。

编辑:作为DataCoord的示例:

[['173', '0.', '0.', '0.'], ['183', '1000.', '0.', '0.'], ['184', '0.', '1000.', '0.'], ['194', '1000.', '1000.', '0.'], ['195', '0.', '0.', '1000.'], ['205', '1000.', '0.', '1000.'], ['206', '0.', '1000.', '1000.'], ['216', '1000.', '1000.', '1000.'], ['217', '0.', '0.', '2000.'], ['227', '1000.', '0.', '2000.'], ['228', '0.', '1000.', '2000.'], ['238', '1000.', '1000.', '2000.'], ['239', '0.', '0.', '3000.'], ['249', '1000.', '0.', '3000.'], ['250', '0.', '1000.', '3000.'], ['260', '1000.', '1000.', '3000.'], ['261', '0.', '0.', '4000.'], ['271', '1000.', '0.', '4000.'], ['272', '0.', '1000.', '4000.'], ['282', '1000.', '1000.', '4000.'], ['283', '0.', '0.', '0.'], ['293', '0.', '1000.', '0.'], ['294', '1000.', '0.', '0.'], ['304', '1000.', '1000.', '0.'], ['305', '0.', '0.', '1000.'], ['315', '0.', '1000.', '1000.'], ['316', '1000.', '0.', '1000.'], ['326', '1000.', '1000.', '1000.'], ['327', '0.', '0.', '2000.'], ['337', '0.', '1000.', '2000.'], ['338', '1000.', '0.', '2000.'], ['348', '1000.', '1000.', '2000.'], ['349', '0.', '0.', '3000.'], ['359', '0.', '1000.', '3000.'], ['360', '1000.', '0.', '3000.'], ['370', '1000.', '1000.', '3000.'], ['371', '0.', '0.', '4000.'], ['381', '0.', '1000.', '4000.'], ['382', '1000.', '0.', '4000.'], ['392', '1000.', '1000.', '4000.'], ['436', '-1000.', '0.', '0.'], ['446', '0.', '0.', '0.'], ['447', '-1000.', '1000.', '0.'], ['457', '0.', '1000.', '0.'], ['458', '-1000.', '1000.', '1000.'], ['468', '0.', '1000.', '1000.'], ['469', '-1000.', '1000.', '2000.'], ['479', '0.', '1000.', '2000.'], ['480', '-1000.', '1000.', '3000.'], ['490', '0.', '1000.', '3000.'], ['491', '-1000.', '0.', '0.'], ['501', '-1000.', '1000.', '0.'], ['502', '-1000.', '1000.', '4000.'], ['512', '0.', '1000.', '4000.'], ['513', '-1000.', '0.', '1000.'], ['523', '0.', '0.', '1000.'], ['524', '-1000.', '0.', '2000.'], ['534', '0.', '0.', '2000.'], ['535', '-1000.', '0.', '3000.'], ['545', '0.', '0.', '3000.'], ['546', '-1000.', '0.', '4000.'], ['556', '0.', '0.', '4000.'], ['557', '-1000.', '0.', '1000.'], ['567', '-1000.', '1000.', '1000.'], ['568', '-1000.', '0.', '3000.'], ['578', '-1000.', '1000.', '3000.'], ['579', '-1000.', '0.', '2000.'], ['589', '-1000.', '1000.', '2000.'], ['590', '-1000.', '0.', '4000.'], ['600', '-1000.', '1000.', '4000.'], ['687', '0.', '2000.', '0.'], ['697', '1000.', '2000.', '0.'], ['698', '0.', '2000.', '1000.'], ['708', '1000.', '2000.', '1000.'], ['709', '0.', '2000.', '2000.'], ['719', '1000.', '2000.', '2000.'], ['720', '0.', '2000.', '3000.'], ['730', '1000.', '2000.', '3000.'], ['731', '0.', '2000.', '4000.'], ['741', '1000.', '2000.', '4000.'], ['742', '0.', '1000.', '0.'], ['752', '0.', '2000.', '0.'], ['753', '1000.', '1000.', '1000.'], ['763', '1000.', '2000.', '1000.'], ['764', '1000.', '1000.', '3000.'], ['774', '1000.', '2000.', '3000.'], ['775', '1000.', '1000.', '0.'], ['785', '1000.', '2000.', '0.'], ['786', '1000.', '1000.', '2000.'], ['796', '1000.', '2000.', '2000.'], ['797', '1000.', '1000.', '4000.'], ['807', '1000.', '2000.', '4000.'], ['808', '-1000.', '1000.', '0.'], ['818', '-1000.', '2000.', '0.'], ['819', '0.', '1000.', '1000.'], ['829', '0.', '2000.', '1000.'], ['830', '0.', '1000.', '2000.'], ['840', '0.', '2000.', '2000.'], ['841', '0.', '1000.', '3000.'], ['851', '0.', '2000.', '3000.'], ['852', '0.', '1000.', '4000.'], ['862', '0.', '2000.', '4000.'], ['863', '-1000.', '2000.', '0.'], ['873', '0.', '2000.', '0.'], ['874', '-1000.', '2000.', '1000.'], ['884', '0.', '2000.', '1000.'], ['885', '-1000.', '2000.', '2000.'], ['895', '0.', '2000.', '2000.'], ['896', '-1000.', '2000.', '3000.'], ['906', '0.', '2000.', '3000.'], ['907', '-1000.', '2000.', '4000.'], ['917', '0.', '2000.', '4000.'], ['918', '-1000.', '1000.', '1000.'], ['928', '-1000.', '2000.', '1000.'], ['929', '-1000.', '1000.', '3000.'], ['939', '-1000.', '2000.', '3000.'], ['940', '-1000.', '1000.', '2000.'], ['950', '-1000.', '2000.', '2000.'], ['951', '-1000.', '1000.', '4000.'], ['961', '-1000.', '2000.', '4000.']]

遍历您的值并收集每个X,Y对的Z坐标; 使用collections.defaultdict可以使此操作变得简单一些,将每个键的默认值set为空set

from collections import defaultdict

levels = defaultdict(set)

for node_id, x, y, z in list_of_datacoords:
    levels[x, y].add(z)

现在, levels是一个将x, y坐标映射到一组唯一z值的字典。

对于您的示例输入,结果为:

>>> pprint(dict(levels))
{('-1000.', '0.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('-1000.', '1000.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('-1000.', '2000.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('0.', '0.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('0.', '1000.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('0.', '2000.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('1000.', '0.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('1000.', '1000.'): set(['0.', '1000.', '2000.', '3000.', '4000.']),
 ('1000.', '2000.'): set(['0.', '1000.', '2000.', '3000.', '4000.'])}

现在,您可以获得每个x, y对的级别的排序列表:

for (x, y), z in levels.iteritems():
    z = sorted(z)
    # now you have x, y and a sorted list of `z` values.

上面的pprint()调用已经按排序顺序打印了集合; 这是因为pprint()默认情况下对python序列的输出进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM