how to search for unique elements by the first column of a multidimensional array

后端 未结 2 794
梦如初夏
梦如初夏 2020-12-12 00:35

I am trying to find a way how to create a new array from a multidimensional array by taking only elements that are unique in the first column, for example if I have an array

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 01:22

    main = [[1, 2, 3], [1, 3, 4], [2, 4, 5], [3, 6, 5]]
    used = []
    new = [[sub, used.append(sub[0])][0] for sub in main if sub[0] not in used]
    print(new)
    # Output: [[1, 2, 3], [2, 3, 4], [3, 6, 5]]
    

提交回复
热议问题