Reputation: 450
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 }
Upvotes: 0
Views: 56
Reputation: 1080
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))
Upvotes: 1