如果满足某些条件,则从元组列表中删除元组 [英] Remove tuple from list of tuples if certain condition is met

查看:33
本文介绍了如果满足某些条件,则从元组列表中删除元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的元组列表;

I have a list of tuples that look like this;

ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD')]

当元组的第一个元素小于 50 时,我想删除元组.OutputList 将如下所示;

I want to remove the tuples when the first element of the tuple is less than 50. The OutputList will look like this;

OutputList = [(100, 'AAA'), (80, 'BBB')]

这在python中如何实现?

How can this be done in python?

非常感谢您的帮助.

推荐答案

您可以轻松地做到:

out_tup = [i for i in in_tup if i[0] >= 50]

[Out]: [(100, 'AAA'), (80, 'BBB')]

这只是创建一个新的元组列表,其中仅包含第一个元素大于或等于 50 的那些元组.结果相同,但方法不同.您接受有效的元组而不是删除无效的元组.

This simply creates a new list of tuples with only those tuples whose first element is greater than or equal to 50. Same result, however the approach is different. Instead of removing invalid tuples you accept the valid ones.

这篇关于如果满足某些条件,则从元组列表中删除元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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