从元组中提取清单

发布于 2025-02-13 02:09:24 字数 282 浏览 1 评论 0原文

我希望从Python的Tuple即时创建一个唯一的列表(例如设置值)。有办法做到吗?

ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

我不想迭代列表,以获取IP地址值。有什么办法可以直接获得IP地址,类似的东西。

ip_addr = ['192.168.1.1','192.168.1.3']

I am looking to create a unique list (like set values) on the fly from tuple in python. Is there a way to do it.

ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

I don't want to iterate over the list, to get the ip address values. Is there any way I can get a setof ip address directly , something like.

ip_addr = ['192.168.1.1','192.168.1.3']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2025-02-20 02:09:24

您可以使用这样的集合理解:

ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

s = {t[0] for t in ip_addrs_with_ports}

...它将给...

{'192.168.1.3', '192.168.1.1'}

You can use a set comprehension like this:

ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

s = {t[0] for t in ip_addrs_with_ports}

...which will give...

{'192.168.1.3', '192.168.1.1'}
孤千羽 2025-02-20 02:09:24
ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

lst = list(set(map(lambda e:e[0],ip_addrs_with_ports)))
print(lst) # ['192.168.1.3', '192.168.1.1']
ip_addrs_with_ports = [('192.168.1.1', '1234'),('192.168.1.1', '1235'),('192.168.1.3', '1236')]

lst = list(set(map(lambda e:e[0],ip_addrs_with_ports)))
print(lst) # ['192.168.1.3', '192.168.1.1']
ι不睡觉的鱼゛ 2025-02-20 02:09:24

使用词典提取和重复IPS:

ip_addr = list(dict(ip_addrs_with_ports))

Using a dictionary to extract and deduplicate the IPs:

ip_addr = list(dict(ip_addrs_with_ports))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文