简体   繁体   中英

Appending to List after Matching

I'm stuck with a rather random problem after inheriting someones work.

There are currently 2 CSV files being imported into the python programme 1 containing numbers 1 containing patterns and a site code

Currently his programme will correctly import both CSV's and check "if pattern in numbers", if so it will print the the number the pattern it matched and then the site code.

The problem is actually that he wants to append each match to a list named after the site code which he had also statically created.

After much googling it seems like globals() would be the option to get the desired behaviour

ie:

x = Range[0]
globals()[x].append(DDI)

where Range[0] is the site code

The issue i seem to be having with this is that all the arrays turn out the same despite the fact that the comparison line is working correctly when printed

Full code for context:

with open('NumPlan.csv', 'r') as f:
    mylist = []
    AEDUB = ATVIE = ATVZR = BEANT = BEBRS = BEBRU = BEMPL = CHBAS = CHLAS = CHZUR = CHWRO = CZOPA = CZPPA = DEBER = DEBSS = DEBRS = DECOL = DEDUS = DEESS = DEFBA = DEFFM = DEFRA = DEHAM = DEMUN = DENUR = DESTU = DKAAR = DKALB = DKCOP = ESBCN = ESMAD = ESMDD = ESPAL = ESVAL = FIHEL = FRBAG = FRBOR = FRLIL = FRLYN = FRMAR = FRMON = FRPRO = FRRHB = FRTOU = GBABE = GBAZT = GBBEL = GBBIR = GBBRI = GBCOL = GBEDI = GBGPH = GBGSH = GBLBS = GBLEE = GBLIV = GBLHH = GBLON = GBLWR = GBMAN = GBNOR = GBPAR = GBSMC = GBSOU = GBTRA = GBTHE = GBXWH = HUBUD = HUGUB = IEDBL = ITMIL = ITMOD = ITMVP = ITROM = ITTUR = ITMIG = LULGI = LULUX = NLAMS = NLUTR = NLDEN = NLRIK = NLROT = NLSCH = NODRO = PLWRO = PLART = PLWAA = PLWKS = PTLIS = ROBUC = RUMOS = SKBRA = SKZAM = SESTG = SESTO = SESOL = TRIST = []
    with open('Ranges.csv', 'r') as R:

     currentDDI = []
     ranges = []
     for line in f:
         li = line.split(",")
         for each in li:
             if "\\" in each:
                 currentDDI.append(each.strip())

     for line2 in R:
         Range = line2.split(",")

         for DDI in currentDDI:
             if Range[1].strip() in DDI.strip():
                 x = Range[0]
                 globals()[x].append(DDI)

     print(GBSMC)

You assigned a single list object to a series of names:

AEDUB = ATVIE = ATVZR = BEANT = ... = SESOL = TRIST = []

All those names now refer to the same list object , not to separate objects. Python names are just labels or name tags , and a value like a list object can have multiple such tags:

>>> AEDUB = ATVIE = ATVZR = []  # 3 names for the same list
>>> AEDUB.append('foo')  # append to the list via one name
>>> ATVIE  # and the same change is visible via another other name
['foo']
>>> ATVZR  # and another
['foo']
>>> ATVIE is ATVZR is AEDUB  # all names refer to the same object?
True

You'd have to type out separate assignments for each name:

AEDUB = []
ATVIE = []
ATVZR = []
BEANT = []
... 
SESOL = []
TRIST = []

However, you really want to avoid using globals() and separate named lists. Use a dictionary instead:

site_codes = {}

and

site_codes.setdefault(Range[0], []).append(DOI)

The dict.setdefault() method takes a key ( Range[0] ) here, and returns the value for that key if it is already present. If it is missing, the second argument is used to set the value in the dictionary first. So if Range[0] not is already in the dictionary, then it'll be set to an empty list, otherwise the existing list is returned. The .append() call then appends to the list for Range[0] .

Note that you never have to actually type out all those names!

You can then use site_codes['AEDUB'] , etc. elsewhere to refer to these lists, or just access a sequence of names with for site_code in site_codes: or access a name and list with for site_code, values in site_codes.values(): , etc. This is far more practical than using global names.

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