[英]Compare if values from a list are greater than X
I have a list like this one, It contains IPs with their name, and the remaining IPs of their range, I would like to only remove those that have more than X IPs in their range available, for example:我有一个像这样的列表,它包含带有名称的 IP,以及它们范围内的剩余 IP,我只想删除那些在其范围内可用的 IP 超过 X 的 IP,例如:
['12.123.0.111/29-name=22', '12.122.2.222/29-name=1', '12.122.2.222/29-name=5']
And I would like to take those values that are less than X, for example, if X is = 5, the list should be:我想取那些小于 X 的值,例如,如果 X = 5,则列表应该是:
['12.122.2.222/29-name=1', '12.122.2.222/29-name=5']
How could it be done with those strings?怎么能用这些字符串来完成呢?
Using a list comprehension使用列表推导
Ex:前任:
data = ['12.123.0.111/29-name=22', '12.122.2.222/29-name=1', '12.122.2.222/29-name=5']
X = 5
result = [i for i in data if int(i.split("=")[1]) <= X ]
print(result)
Output: Output:
['12.122.2.222/29-name=1', '12.122.2.222/29-name=5']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.