简体   繁体   中英

How do I append the value of a variable (not the reference) into a list in Python 3.x?

I always end up with duplicates of the variable pp in the list pplist.ppli because python just stores references of variables. See below for code snippet. i can't figure out how to store the value of pp into the list pplist.ppli.

import XYZ

paths = [500,1000,2000,3000,4000,5000,6000,7000,8000]
seeds = [1,125000,250000,375000,500000,625000,750000,875000,1000000]
pplist = XYZ.get_pplist({"expression":"name = Standard"})

for seed in seeds:
    for path in paths:
        pp = pplist.ppli[0]
        rule = [r for r in pp.someList if r.product_Code == "TEXT"]
        rule[0].Parameters["Seed"].nonTabularValue = seed
        rule[0].Parameters["Paths"].nonTabularValue = path
        pp.someList.insert(0,rule[0])
        pp.name = "Seed"+str(seed)+"Paths"+str(path)
        pplist.ppli.append(pp)

XYZ.set_pplist(pplist)

What you need is a copy of the data (in this case a dict ):

import copy
...
for seed in seeds:
    for path in paths:
        pp = copy.copy(pplist.ppli[0])            ...

In case you have inner references you may have to consider using deepcopy

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM