How to Update the value of a variable?

Asked

Viewed 774 times

2

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

`def Pegavalor(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`
  • 1

    just put self.v that you will be able to use it in every class, including, you can use v[0] = 1 to replace the value of the first element to 1

1 answer

4


It is difficult to understand your context with this code fragment, 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, isn’t it? at least looking at the code that you passed, that’s what it looks like. By the way, it seems to me that you came from another language, I recommend you see the PEP8, the style guide for python encoding.

I developed a function that receives a list and a dictionary with the variables (in pythonic style) that you have posted and replace in the list according to the value of the variable, as I understand it, the value of the list will be changed to 1 if the value of the variable is equal to 1, then it was 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 few hours that I posted the above solution, I returned and saw how much verbiage it got, 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)

Look at this new code here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.