dict_3 = {k: v for k, v in big_dict.items() if v == 3}
第二步一样,让我们随机化键,然后分为80%和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}
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}
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
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()
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
如果不清楚,我会很高兴为您提供帮助!
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!
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]
Your question must be Finding All The Keys With the Same Value in a Python Dictionary [duplicate]
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]
发布评论
评论(4)
第一步很容易,就像
第二步一样,让我们随机化键,然后分为80%和20%:
First step is easy, something like
For the second step, let's randomize the keys and then split into 80% and 20%:
循环的一些基本要介绍:
从那里您可以使用 dict。键()
Some basic for loop should cover it:
From there you can select the percentual of keys at random using dict.keys()
这是我的解决方案!
如果不清楚,我会很高兴为您提供帮助!
This is my solution!
If something is not clear, I'll be glad to help you!
您的问题一定是在Python字典中找到具有相同值的所有键[重复]
这是链接
所有具有相同值的键在Python词典中
您的代码
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