按键和百分比分开python词典

发布于 2025-01-24 10:50:40 字数 1399 浏览 0 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夕色琉璃 2025-01-31 10:50:40

第一步很容易,就像

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}


兔小萌 2025-01-31 10:50:40

循环的一些基本要介绍:

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

从那里您可以使用 dict。键()

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()

无所谓啦 2025-01-31 10:50:40

这是我的解决方案!

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!

孤凫 2025-01-31 10:50:40

您的问题一定是在Python字典中找到具有相同值的所有键[重复]

这是链接
所有具有相同值的键在Python词典中

您的代码

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文