2

I'm in the process of writing a program for the robotics team I'm apart of to gather data during competition to find the best team to "alliance" with. It needs gather about 20 values and needs to be able to list data by various parameters. I have a lot of the framework done but it seems like when I append my data to the list it will change all entries to when I'm appending.

For example, if I have

data = [{"teamNumber":1345,"teamName":"Blah"}]

and try to append

{"teamNumber":3219,"teamName":"TREAD"}

to data using append() I end up with

data = [{"teamNumber":3219,"teamName":"TREAD"},{"teamNumber":3219,"teamName":"TREAD"}]

Currently we are adding data using this function(It has most of the unnecessary "meat" trimmed)

def inputTeamData(): 
    global data
    clear()
    temp = dataTemplate

    temp["teamNumber"] = question("Team Number?: ")
    temp["teamName"] = question("Team Name?: ")
    temp["foundingSeason"] = question("Founding Season?: ")

    save = question("Is the above data correct?(y/n): ")


    if save.lower() == "y":
        try:
            data.append(temp)
        except ValueError:
            print "ValueError. You gave me the wrong data types"
            sleep(1)

            else:
                print "Not saved"
                sleep(.8)

I don't know what gives, even though the answer is more then likely to be stupidly obvious I can't seen to find the problem.

Anyways, much thanks in advice to whoever can help me with this!

4

1 回答 1

6

You need to learn some Python basics first:

temp = dataTemplate

does not copy dataTemplate to a new variable temp! Instead it gives the alias "temp" to the original dictionary which you had named "dataTemplate". You modify the same original dictionary each time when you write temp[ .. ] = because "temp" is just a alias for "dataTemplate".

You want

temp = dataTemplate.copy()
于 2013-03-16T20:41:31.713 回答