简体   繁体   中英

Duplicating dictionary with random values

I have a sample dictionary which I want to duplicate with random values to create test data. I am using random function to generate values but it is not working as expected. As I want to populate list in side dictionary as well as create different keys as well. Below is my sample dict.

a = {
  "6335050000000172": [
    {
      "accBalance": 6,
      "accNum": 6335050000000172,
      "activityCode": 440,
      "activityDate": "2021-09-27 00:00:00",
      "creditsDebits": 0,
      "displayPostage": "",
      "meterSerialNumber": 0,
      "postage": 0,
      "timeZone": "",
      "transactionDescription": "PAYMENT THANK YOU"
    }
  ]
}

My duplicating code:

times = ["2021-09-27 00:00:00", "2021-08-27 00:00:00","2021-07-27 00:00:00","2021-06-27 00:00:00","2021-05-27 00:00:00","2021-03-27 00:00:00"]

c = ["6335050000000172","6335052000143333","6335051000136685","6335052030050953","6335050000000313","6335052030050185"]
transactionDescription = ['DIRECT DEBIT PAYMENT THANK YOU', 'METER REFILL', 'METER RESET', 'PAYMENT RECEIVED' ]
b = {}

i = 0
while i <= 3:
#for i in range(100):
    for j in a.values():
        j[0]['accBalance'] = random.randint(0,100)
        j[0]['transactionDescription'] = random.choice(transactionDescription)
        j[0]['accNum'] = random.choice(c)
        j[0]['activityDate'] = random.choice(times)
        if j[0]['accNum'] in b.keys():
            j.append(j[0]['accNum'])
        else:
            b[j[0]['accNum']] = j
        #print(b)
        i = i + 1



print(b)
#f = open("output.txt", "a")
#print(b, file=f)

#f.close()

I am trying to get a sample output as below:

{'6335050000000172': [{...},{...},...m], '6335052000143333': [{..}...m] ......., n }

Maybe like this?

import random
from collections import defaultdict


def get_data(n):
    res = defaultdict(list)
    c = ["6335050000000172",
         "6335052000143333",
         "6335051000136685",
         "6335052030050953",
         "6335050000000313",
         "6335052030050185"]
    times = [
        "2021-09-27 00:00:00",
        "2021-08-27 00:00:00",
        "2021-07-27 00:00:00",
        "2021-06-27 00:00:00",
        "2021-05-27 00:00:00",
        "2021-03-27 00:00:00"
    ]

    transactionDescription = ['DIRECT DEBIT PAYMENT THANK YOU', 'METER REFILL', 'METER RESET', 'PAYMENT RECEIVED']

    for i in range(n):
        title = random.choice(c)
        res[title].append(
            {
                "accBalance": random.randint(0,100),
                "accNum": title,
                "activityCode": 440,
                "activityDate": random.choice(times),
                "creditsDebits": 0,
                "displayPostage": "",
                "meterSerialNumber": 0,
                "postage": 0,
                "timeZone": "",
                "transactionDescription": random.choice(transactionDescription)
            }
        )
    return res


print(get_data(10))

enter image description here

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