If I have a list like this:
[element(name='A', value=0), element(name='B', value=1)]
I would like to find all the element of the list whose value field is equal to 0:
element[:].value == 0
Which would the shortest way?
elements = [element(name='A', value=0), element(name='B', value=1)]
matches = [element for element in elements if element.value == 0]
To get the names from all the matches:
names = [element.name for element in matches]
print(names)
A list comprehension should do the trick
not_values = [e for e in elements if not e.value]
Please note I'm using not e.value
because I think it reads better and it's more coherent with the variable name not_values
but you could also use e.value == 0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.