Home > Net >  Python Randomly Rename values in dicts who are nested in Dict[Array]
Python Randomly Rename values in dicts who are nested in Dict[Array]

Time:04-25

after hours of guesswork I give up. Below you see my code and I want do describe it as good as possible.

I have following setup:

self.dict_ is a dict with two key value pairs.

test_0 is a dummy without any significance

test_1 is an array

In range of self.controller I want to put copies from self.test_dict into test_1.

In this case 5 times. So far so good, no problem.

Now comes the part in which everything goes wrong. For every copy of test_dict in test_1 I want to change randomly ID and name. And that doesn't work. Every name and ID gets the same value.

When I hardcode self.dict_ as

    {
   "test_0": "", "test_1": [
    {"name": "", "ID": 0}, 
    {"name": "", "ID": 0}, 
    {"name": "", "ID": 0}, 
    {"name": "", "ID": 0}, 
    {"name": "", "ID": 0}
        ]
        }

everything works fine and each name and ID is changed randomly.

Like I said, after hours of thinking I am out of options and need help urgently.

The for loop in test_run is necessary. For testing and showing you what I mean I adopted the concept from my real setup and in that setup it is necessary and working fine.

Thanks in advance.

Code with print statements for test purposes:

import random


class Bla:

    def __init__(self):
        self.dict_ = {"test_0": "", "test_1": []}
        self.test_dict = {"name": "", "ID": 0}
        self.name_list = ["Daniel", "Eva", "Adam", "Bert", "Ernie"]
        self.random_num = [0, 1, 2, 3, 4, 5]
        self.controller = 5

    def append(self):

        r = 0

        for x in self.dict_['test_1']:

            random_name = random.sample(self.name_list, 1)
            x['name'] = random_name[0]
            random_number = random.sample(self.random_num, 1)
            x['ID']  = random_number[0]
            r  = 1
            print("r", r)
            print("innner", self.dict_['test_1'])

    def test_run(self):

        for i in range(1):
            v = 0
            for i in range(self.controller):
                self.dict_['test_1'].append(self.test_dict)
                v  = 1
                print(self.dict_, "v", v)
            self.append()
            

            print(self.dict_['test_1'])


z = Bla()
z.test_run()

Code without print statements:

import random


class Bla:

def __init__(self):
    self.dict_ = {"test_0": "", "test_1": []}
    self.test_dict = {"name": "", "ID": 0}
    self.name_list = ["Daniel", "Eva", "Adam", "Bert", "Ernie"]
    self.random_num = [0, 1, 2, 3, 4, 5]
    self.controller = 5

def append(self):

    for x in self.dict_['test_1']:

        random_name = random.sample(self.name_list, 1)
        x['name'] = random_name[0]
        random_number = random.sample(self.random_num, 1)
        x['ID']  = random_number[0]

def test_run(self):

    for i in range(1):

        for i in range(self.controller):
            self.dict_['test_1'].append(self.test_dict)
        self.append()



z = Bla()
z.test_run()

CodePudding user response:

I think deepcopy might be what you are looking for.

import random
from copy import deepcopy

class Bla:

    def __init__(self):
        self.dict_ = {"test_0": "", "test_1": []}
        self.test_dict = {"name": "", "ID": 0}
        self.name_list = ["Daniel", "Eva", "Adam", "Bert", "Ernie"]
        self.random_num = [0, 1, 2, 3, 4, 5]
        self.controller = 5

    def append(self):

        for x in self.dict_['test_1']:

            random_name = random.sample(self.name_list, 1)
            x['name'] = random_name[0]
            random_number = random.sample(self.random_num, 1)
            x['ID']  = random_number[0]

    def test_run(self):

        for i in range(1):

            for i in range(self.controller):
                self.dict_['test_1'].append(deepcopy(self.test_dict))
            self.append()



z = Bla()
z.test_run()

print (z.dict_)

{'test_0': '', 'test_1': [{'name': 'Eva', 'ID': 3}, {'name': 'Eva', 'ID': 3}, {'name': 'Ernie', 'ID': 3}, {'name': 'Daniel', 'ID': 0}, {'name': 'Adam', 'ID': 5}]}

However, as you can see the way how you randomly select ID/name is likely to create duplicates, but since you didn't state if you want/doesn't want duplicates, I'll just leave it as is for now.

  • Related