-1

I habe a python dictionary like this one with 4 different value categories:

big_dict = {'num1' : '4', 'num5' : '1', 'num23' : '3', 'num2' : '3',...}

What i want to do ist split the big dictionary by the four value categories:

dict_3 = {'num23' : '3', 'num2' : '3',....}
dict_1 = {}
...

After that i want to split each of these smaller into new dicts each containing 80% and 20% of the data. So basically like that:

dict_3_80 = 80% of dict_3
dict_3_20 = 20% of dict_3
...

Thanks in advance!

stp2058
  • 41
  • 5

4 Answers4

1

First step is easy, something like

dict_3 = {k: v for k, v in big_dict.items() if v == 3}

For the second step, let's randomize the keys and then split into 80% and 20%:

import random

keys_list = list(dict_3.keys())  # shuffle() wants a list
random.shuffle(keys_list)  # randomize the order of the keys

nkeys_80 = int(.8 * len(di))  # how many keys does 80% equal
keys_80 = keys_list[:nkeys_80]
keys_20 = keys_list[nkeys_80:]

# create new dicts
dict_3_80 = {k: dict_3[k] for k in keys_80}
dict_3_20 = {k: dict_3[k] for k in keys_20}


Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
0

Some basic for loop should cover it:

splits = [{}, {}, {}, {}]        # Create the set of dictionary
for k, v in big_dict.items():    # Iterate over your big_dict
    splits[int(v) - 1][k] = v    # Set element to the correct dict

From there you can select the percentual of keys at random using dict.keys()

Deusy94
  • 733
  • 3
  • 13
0

This is my solution!

big_dict = {'num1' : '4', 'num5' : '1', 'num23' : '3', 'num2' : '3', "num" : "2","num7":"2"}
dict_3 = dict()
dict_2 = dict()
for index,(key,value) in enumerate(big_dict.items()):
    if index <= (len(big_dict)/100*80-1): 
    # if you want you can round the value above with round() function
    # -1 is there because indices start from 0
        dict_3[key] = value
    else:
        dict_2[key] = value

If something is not clear, I'll be glad to help you!

Jock
  • 388
  • 2
  • 12
0

Your question must be Finding All The Keys With the Same Value in a Python Dictionary [duplicate]

this is link Finding All The Keys With the Same Value in a Python Dictionary

your code

dict_4=[k for k,v in big_dict.items() if v ==4]
dict_1 = [k for k, v in big_dict.items() if v == 1]
dict_3 = [k for k, v in big_dict.items() if v == 3]
Mr.F.K
  • 21
  • 1
  • 3