【发布时间】:2015-11-02 15:47:31
【问题描述】:
谁能提供有关如何按某些列表元素查找和分组列表的建议?
下面是一个示例,它通过比较所有列表元素在子列表中查找和分组相同的值...
在列表中查找和分组相同值的工作示例:
L = #List of Vectors
# Example L[0] = [8.999999590709139, 164.00000059986633, 152.00000160567942]
d = defaultdict(list) # Group by same Vectors
for item in L:
d[tuple(item)].append(item)
sv = sorted(d[x] for x in d)
print "No of same values found =”, len(sv)
上面的工作示例考虑了列表中的所有项目进行比较,但是如何考虑仅通过列表的前三个项目来查找和分组相同的项目,同时保持列表完整?
# Example L2[0] = [8.999999590709139, 164.00000059986633, 152.00000160567942, "Q","W","E","R","T","Y","3.14"]
# Example L2[1] = [8.999999590709139, 164.00000059986633, 152.00000160567942, "A","S","D","F","G","H","J"]
# len(L2[0]) = 10
# Find and group lists with same L2[i][0], L2[i][1], L2[i][2] values...
# Desired output - sortedlist = [[[1,2,3,"a","b","c"],[1,2,3,"d","e","f"]],[[3,4,5,"a","b","c"],[3,4,5,"d","e","f"]]]
#print sortedlist returns [[[1, 2, 3, 'a', 'b', 'c'], [1, 2, 3, 'd', 'e', 'f']], [[3, 4, 5, 'a', 'b', 'c'], [3, 4, 5, 'd', 'e', 'f']]]
#print sortedlist[1] returns [[3, 4, 5, 'a', 'b', 'c'], [3, 4, 5, 'd', 'e', 'f']]
#print sortedlist returns [1][2] [3, 4, 5, 'a', 'b', 'c']
任何帮助或指点将不胜感激......
【问题讨论】:
-
the first three items of a list->my_list[:3] -
我尝试了几种方法,对于 L 中的项目:d[tuple(item[0],item[1],item[2)].append(item) 等...不幸的是没有作业,我是一名合格的建筑师,正在尝试使用矢量和数据对复杂的几何图形进行建模!
标签: python list sorting tuples