简体   繁体   English

在Python中对具有相同值的列表和子列表进行排序和分组

[英]Sorting and grouping lists and sub lists with the same values in Python

I'm wondering can anyone help me with this problem, I feel so close yet so far.... I can't seem to get my head around this. 我想知道有人能帮助我解决这个问题吗,我觉得到目前为止还很亲近。。。我似乎无法解决这个问题。

I have a list of 3D vectors (X,Y,Z) to be sorted by columns - many of the X values are the same. 我有一个按列排序的3D向量(X,Y,Z)列表-许多X值是相同的。

# Example list (L)

L = [1,7,9], [2,4,9], [2,0,10], [1,12,9], [9,9,1], [4,6,2], [7,6,2], [4,12,6], [5,7,1]

# Desired result
R = [[1,7,9], [1,12,9]], [[2,4,9], [2,0,10]],[[4,6,2], [4,12,6]], [5,7,1], [7,6,2], [9,9,1]

# Example calling individual columns (real values expected to be in the 10's)

R[0] = [[1,7,9], [1,12,9]] # A column 2 high
R[3] = [5,7,1] # A column 1 high

Working example on a single list 单个列表上的工作示例

Using the Counter function in the collections module and with some much appreciated help on here, the following code can sort a single list: 使用collections模块中的Counter函数,并在此获得一些非常赞赏的帮助,以下代码可以对单个列表进行排序:

 from collections import Counter

 N = [2,5,7,9,2,8,5,2,7,9]
 C = Counter(N)

 print [ [k,]*v for k,v in C.items()]
 # Returns [[8], [9, 9], [2, 2, 2], [5, 5], [7, 7]]

I tried linking the Y and Z values back to newly grouped X vectors, however I ran into indexing issues, as the indices of X list changed. 我尝试将Y和Z值链接回新分组的X向量,但是当X列表的索引发生更改时,我遇到了索引问题。

Any help on this would be much appreciated, so far this is my attempt and the direction I'm exploring... (passing values into functions) 对此的任何帮助将不胜感激,到目前为止,这是我的尝试以及我探索的方向...(将值传递给函数)

from collections import Counter

N = [1,7,9], [2,4,9], [2,0,10], [1,12,9], [9,9,1], [4,6,2], [7,6,2], [4,12,6], [5,7,1]


def compareXvalues(VectorList):
    global Grouped_X_Vectors

    Xvectors = []
    for i in xrange (len(VectorList)):
       Xvectors.append(VectorList[i][0])

    C = Counter(Xvectors)
    Grouped_X_Vectors = [ [k,]*v for k,v in C.items()]

    for i in xrange (len(Grouped_X_Vectors)):
        #print Grouped_X_Vectors[i]
        pass

print N

compareXvalues(N)
print Grouped_X_Vectors

Any feedback or help would be much appreciated, my brain is fried. 任何反馈或帮助将不胜感激,我的大脑被炸了。

You can accumulate them by X value in a dictionary and then sort the results into a list. 您可以按字典中的X值累加它们,然后将结果排序到一个列表中。 In my example I use a defaultdict since I want to call append on the items of the dictionary and this prevents me from needing to initialize a list for each value of X that I encounter. 在我的示例中,我使用defaultdict,因为我想在字典的项目上调用append,这使我无需为遇到的每个X值初始化一个列表。

>>> from collections import defaultdict
>>> L = [[1,7,9], [2,4,9], [2,0,10], [1,12,9], [9,9,1], [4,6,2], [7,6,2], [4,12,6], [5,7,1]]
>>> d = defaultdict(list)
>>> for item in L:
        d[item[0]].append(item)

>>> R = sorted(d[x] for x in d)
>>> R
[[[1, 7, 9], [1, 12, 9]], [[2, 4, 9], [2, 0, 10]], [[4, 6, 2], [4, 12, 6]], [[5, 7, 1]], [[7, 6, 2]], [[9, 9, 1]]]

I know this is a bit different from the path you were taking, but the dictionary fulfills the basic idea you had of "linking" your Y and Z values to your X value. 我知道这与您采用的路径有些不同,但是字典实现了您将YZ值“链接”到X值的基本思想。

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

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