在python中获取一组二维列表 [英] Get a set of 2d list in python

查看:66
本文介绍了在python中获取一组二维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下列表

t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]]

我需要从中取出一套,这样输出才是

I need to get a set out of this so the output would be like

t=[[1, 7], [3, 7], [5, 8]]

我尝试使用 t = set(t),但是没有用

I tried to use t=set(t) but it didn't work

推荐答案

如果您不关心订单,可以先使用以下命令将内部 lists 转换为 tuples map()函数,然后将它们转换为 set ,然后返回到 list .

If you do not care about the order, you can first convert the inner lists to tuples using map() function, and then convert them to set and then back to list .

示例-

>>> t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]]
>>> t = list(set(map(tuple,t)))
>>> t
[(3, 7), (5, 8), (1, 7)]

这篇关于在python中获取一组二维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆