0
a1 = 2
a2 = 4
a3 = 5
xa1 = 0
xa2 = 0
xa3 = 0
d = {a1 : xa1, a2 : xa2}
l1 = [a1, a2]
for item in l1:
  d[item] += item

In this code, I want the value of a1 to be in xa1, and a2 to be in xa2, but not for xa3.

I tried to do it this way but this might not be a right way to do it. My way of doing this is not correct, so if there is another way, please help me out.

(Note that there are 81 variable like a1, a2 and so on, and 81 like xa1, xa2 and so on and they are selected randomly to have the value copied from 'a' variables)

I hope I didn't make a mess and people can understand what I'm trying to say.

  • 1
    Welcome to Stack Overflow! Please take the [tour] and read the [help], and ask on [meta] if you have any questions :) – Ekadh Singh - Reinstate Monica Dec 17 '21 at 17:34
  • Instead of making a bunch of different variables, do `a = [2, 4, 5]` and `xa = [0, 0, 0]`. Now you can copy subsets of `a` into `xa`, e.g. `xa[0:2] = a[0:2]`! – Samwise Dec 17 '21 at 17:39
  • When you create a dictionary from variables, the dictionary just gets the variable values, it's not a reference to the variables. So updating the dictionary doesn't have any effect on the variables. – Barmar Dec 17 '21 at 17:47
  • @Barmar Oh yes, I think this is what happened, I did do it like this till the last step, but when I used the variable xa1 instead of the dictionary reference to the same, it just gave me the original value (that it 0). Thank you! – Suryanshsinh Sisodiya Dec 17 '21 at 18:03
  • 1
    @Samwise Yeah, now that I think about it, doing it like this would've been way easier. Thank you! – Suryanshsinh Sisodiya Dec 17 '21 at 18:03

1 Answers1

0

try

for item in l1: d[item] = item

i removed the + from += item because i think you're trying to update the xa* values

T.Galisnky
  • 79
  • 1
  • 11
  • That's the same as the code in the question. – Barmar Dec 17 '21 at 17:36
  • He didn't write it very well, but I think he's actually trying to set the variables `xa1` and `xa2`, not update the dictionary. – Barmar Dec 17 '21 at 17:38
  • my bad, check now, i only put a short part so i had to write more to be able to post, and when i copied a larger chunk i forgot to replace it – T.Galisnky Dec 17 '21 at 17:38
  • 1
    You should explain what you changed and why it will solve the problem. don't just post code – Barmar Dec 17 '21 at 17:39
  • ok i changed the + sign, just tryina help out man if he want to set the variables he can just use another list for xa* like he did for a* – T.Galisnky Dec 17 '21 at 17:42
  • 1
    You should put that in the answer, not a comment. And explain WHY you did it. – Barmar Dec 17 '21 at 17:42
  • This doesn't update the xa variables, it just updates the dictionary. There's no link between them. – Barmar Dec 17 '21 at 17:46
  • yeah but then he can access it however he likes, he already got them sharing indexes a1 is xa1 and so on, so using the same index he can easily get it from the dictionary instead, he can also declare xa1 with it later if he wanted – T.Galisnky Dec 17 '21 at 17:49
  • I have a situation like, there is a list of a* variables with random values, and some of them are selected randomly and I want to transfer the values which they had to xa* variables (only the ones selected randomly). I don' know if using a dictionary solves the problem or its just the way I use it is stupid. – Suryanshsinh Sisodiya Dec 17 '21 at 17:55
  • how are some of them selected randomly ? can you catch the ones who are ? if so then taking those values is easy, if not then your issues is with differentiating between those selected randomly and those not – T.Galisnky Dec 17 '21 at 17:59
  • @T.Galisnky, actually, for example, I got a5 randomly with int value 3 in it. Now I want the exact same int value in xa5 only, and not in any other variable like xa3 or xa4, and since xa3 and xa4 are not chosen randomly (in this example), I don't want their values to be changed (that is remain 0). – Suryanshsinh Sisodiya Dec 17 '21 at 18:08
  • so want an 'old' list and a 'new' list, where a1 is in old and xa1 is in new, where xa1 keeps its original value if its not changed randomly and assigned the new value if changed randomly – T.Galisnky Dec 17 '21 at 18:10
  • Oh yes! I'll try it like this, 2 lists... So I just need to get the indexing value of the randomly selected variable and assign the value to the xa* list with same index. I think it'll work, thank you! – Suryanshsinh Sisodiya Dec 17 '21 at 18:19