Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Dictionary content changes without declaration

I have a Problem in this function:

def show_selected_button(self):
        attrbs = required_attributes[self.modeComboBox.currentText()]
        layers = QgsProject.instance().mapLayersByName(self.layerComboBox.currentText())
        layer = layers[0]
        layer.selectAll()
        selected_features = layer.getSelectedFeatures()
        data = []
        for i in selected_features:
            attribute_row = []
            for a in attrbs:
                attrb = i.attribute(name=a)
                if a == "MITTLEREBR":
                    attrb = round(attrb, 2)
                attribute_row.append(attrb)

            laenge = i.geometry().length()
            vol = laenge * i.attribute(name="DICKE") / 100 * i.attribute(name="MITTLEREBR")
            attribute_row.append(round(laenge, 2))
            attribute_row.append(round(vol, 2))
            data.append(attribute_row)

        attrbs.append("LÄNGE")
        attrbs.append("VOLUMEN")
        self.tableWidget.setRowCount(len(data))
        self.tableWidget.setColumnCount(len(attrbs))
        self.tableWidget.setHorizontalHeaderLabels(attrbs)

        for i, x in enumerate(data):
            for j, y in enumerate(x):
                self.tableWidget.setItem(i, j, QtWidgets.QTableWidgetItem(str(y)))

When I execute this function for the first time (when I click the button), everything works fine but when I use one more time after the first usage, required_attributes["Strasse"] includes the values "LÄNGE" and "VOLUMEN" that originally got added to attrbs and never got added to the dictionary.

Content of required_attributes["Strasse"] when function is run for the first time:
['EBENE', 'SCHICHTTYP', 'SCHICHTT01', 'SCHICHTT02', 'DICKE', 'MITTLEREBR']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Content of required_attributest["Strasse"] when function is run a second time:
['EBENE', 'SCHICHTTYP', 'SCHICHTT01', 'SCHICHTT02', 'DICKE', 'MITTLEREBR', 'LÄNGE', 'VOLUMEN']

required_attributes is a variable stored in a different file.

I don’t know why this is happening, can anyone help me?

>Solution :

By assigning to attrbs value of required_attributes[self.modeComboBox.currentText()] you’ve created a link to the same object in memory, so whenever you change attrbs now you are changing list inside dictionary.

attrbs = required_attributes[self.modeComboBox.currentText()]

Check this answer for an example: https://stackoverflow.com/a/13531087/10438425

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading