If I have a vector layer in QGIS, how do I use Python to find the maximum value which a given field has? I need to convert the values in one field into values between 0 and 1. I guess I therefore need to set it to
val/maxVal. That's why I need to find the maximum value.
It is not necessary to get a complete list of field values. In QgsVectorLayer exists 'maximumValue' method. So, this works well and it's shorter:
layer = iface.activeLayer() idx = layer.fieldNameIndex('fieldName') print layer.maximumValue(idx)
You could use the following in the Python Console to print out the maximum value for a given field:
values =  layer = qgis.utils.iface.activeLayer() idx = layer.fieldNameIndex('fieldName') for feat in layer.getFeatures(): attrs = feat.attributes() values.append(attrs[idx]) print max(values)