I have been using this script to select a feature using Python:
layer = iface.activeLayer()
layer.selectByExpression('\"Declividad\"= value', QgsVectorLayer.SetSelection)
selection = layer.selectedFeatures()
But now, I need to select the values for Declividad from a list.
For exemple: list = [10,11,12], so I want to select the values 10,11 and 12 for Declividad.
How could I do that?
Snaileater
5,8031 gold badge18 silver badges27 bronze badges
asked Jun 10, 2019 at 14:25
2 Answers 2
You could juse use an expression like:
layer = iface.activeLayer()
my_list = [10, 11, 12]
values = ','.join(str(x) for x in my_list)
layer.selectByExpression('\"Declividad\" IN (' + values + ')', QgsVectorLayer.SetSelection)
answered Jun 10, 2019 at 14:43
-
@caiovillaca - Most welcome! Glad it helped :)Joseph– Joseph2019年06月11日 10:36:13 +00:00Commented Jun 11, 2019 at 10:36
You can try the IN operator :
layer.selectByExpression('\"Declividad\" IN (\'10\',\'11\','12円')', QgsVectorLayer.SetSelection)
Or do u need to dynamically reference the list name ?
answered Jun 10, 2019 at 14:42
-
yep, I need it to read the values from the list...Kajo– Kajo2019年06月10日 14:51:36 +00:00Commented Jun 10, 2019 at 14:51
lang-py