python - 如何在python中合并列表中的重复元素?

标签 python python-2.7

我有一个坐标列表:

list_coordinate =[(9,0),(9,1),(9,3) ... (53,0),(53,1),(53,3)...(54,0),(54,1)..]

value = []

 for m in range(0,len(list_coordinate)):    
    if m != len(list_coordinate)-1:
        if list_coordinate[m][0]==list_coordinate[m+1][0]:
            value.append(list_coordinate[m][0])`

这段代码的输出:

value = [9,9 ,9,...,53,53,53,...,54,54,54,54...]

我想合并类似元素的这个值列表,并希望输出为:

预期输出:

 [9,53,54]

最佳答案

如果你喜欢单行,你可以这样做:

list(set(map(lambda x: x[0], list_coordinate)))

它会输出:

[9, 53, 54]

注意:由于代码中使用了集合,因此这里不保证元素的顺序。

关于python - 如何在python中合并列表中的重复元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33118846/

相关文章:

python - 为什么 numpy 列表访问比 vanilla python 慢?

python - 计算 Python 中随机调用的次数?

python - 将 3D 列表转换为 3D NumPy 数组

python - Python 中的 Tab 键不缩进

python - 如何识别在 python 中从 set() 中删除的元素?

python-2.7 - 没有名为 http.server 的模块

python - 将带有分隔符的数据框展平为变量

python - scipy.stats.kstest 与规范以外的分布

python - 从另一个字典列表中减去字典列表中的值

python - 使用 "and"作为返回