How to update the value of a variable?

2

I made a function in which it has an initial variable and according to which the user marks a checkbutton it takes this value stored in the variable and then takes and verifies the value of it and deletes the previous value puts the current value. but my question is, how do I update the variable or get the final value of it?  follow the code

'def GetValue (self):

    v = [0,0,0,0]
    if self.VnomeValor == 1:
      v.remove(v[0])
      v.insert(0, 1)
    else:
      pass

    if self.VcorValor == 1:
        v.remove(v[1])
        v.insert(1, 1)
    else:
      pass
    if self.VcpfValor == 1:
        v.remove(v[2])
        v.insert(2, 1)
    else:
        pass
    if self.VemailValor == 1:
        v.remove(v[3])
        v.insert(3, 1)
    else:
        pass'
    
asked by anonymous 19.05.2017 / 00:31

1 answer

4

Difficult to understand its context with this fragment of code, but what it seems is that you just want to make a "replace" in the list according to the values of the variables VnomeValor, VcorValor, etc, is not it? at least by looking at the code you passed, that's what it looks like. By the way, I think you came from another language, I recommend you check out PEP8, the style guide for python encoding .

I developed a function that receives a list and a dictionary with the variables (in the pythonic style) that you have posted and makes replace in the list according to the value of the variable, as far as I understand, the value of the list will be changed to 1 if the value of the variable is equal to 1, then it looks like this:

def get_list(_data, _list1):
     if _data['cor_valor']['value']==1:       
         _list1[ data['cor_valor']['pos'] ]
     if data['cpf_valor']['value']==1:
         _list1[data['cpf_valor']['pos']]=1
     if data['mail_valor']['value']==1:
         _list1[data['mail_valor']['pos']]=1
     if data['nome_valor']['value']==1:
         _list1[data['nome_valor']['pos']]=1
     return _list1   

data = {'cor_valor': {'pos': 1, 'value': 0},
        'cpf_valor': {'pos': 2, 'value': 1},
        'mail_valor': {'pos': 3, 'value': 0},
        'nome_valor': {'pos': 0, 'value': 1}
       }

list1 = [0,0,0,0]

list1 = get_list(data, list1)
print (list1)
[1, 0, 1, 0] 

See the code working here.

  

After a couple of hours I posted the above solution, I returned and saw how verbal it was, so I decided to reduce it, see:

data = {'cor_valor': {'pos': 1, 'value': 0},
        'cpf_valor': {'pos': 2, 'value': 1},
        'mail_valor': {'pos': 3, 'value': 0},
        'nome_valor': {'pos': 0, 'value': 1}
       }

list0 = [0,0,0,0]

def get_list_v2(_data, _list1):
    for d in _data:
        if _data[d]['value']==1:
            _list1[data[d]['pos']]=1
    return _list1        

list1 = get_list_v2(data, list0)
print (list1)

View this new code here.

    
19.05.2017 / 15:03