简体   繁体   English

如何将字典分成几部分?

[英]How would I split a dictionary up into parts?

So I'm wanting to split a dictionary up into parts of 19, This is only for one thing so it wouldn't remain or be saved it'll just be part of a program so it'll split them into pieces of 19 so if theres 28 it'll split them into two parts one 19 the other 9. 所以我想将字典分割成19个部分,这只是为了一件事,所以它不会保留或保存,它只是程序的一部分,所以将它们分割成19个部分,所以如果有28,则将其分为两部分,一个19,另一个9。

If that makes sense at all just struggling how to go about this xD example: 如果那完全有道理,那么请努力解决该xD示例:

1- "69angel": {"uid": "u99bd5055900298f13821ad6" }, "bestbrittany": { "uid": "u80d4520e66090a088a8e73b2af" }, 1- "69angel": {"uid": "u99bd5055900298f13821ad6" }, "bestbrittany": { "uid": "u80d4520e66090a088a8e73b2af" },

2- "blegh": { "uid": "u0c4afad0a1e1d3b9ffdd3db444d" }, "cassie84": { "uid": "u9b53c15bfd2f0e5a3741be1297" }, 2- "blegh": { "uid": "u0c4afad0a1e1d3b9ffdd3db444d" }, "cassie84": { "uid": "u9b53c15bfd2f0e5a3741be1297" },

etc etc etc 等等等

If you wanted to spit the dict = below up into two's it'll be as so ^ 如果您想将dict =吐到下面,将其加为2,就这样^

dict = 字典=

    "selfbots": {
        "69angel": {"uid": "u99bd5055900298f13821ad6"
        },
        "bestbrittany": {
            "uid": "u80d4520e66090a088a8e73b2af"
        },
        "blegh": {
            "uid": "u0c4afad0a1e1d3b9ffdd3db444d"
        },
        "cassie84": {
            "uid": "u9b53c15bfd2f0e5a3741be1297"
        },
        "charlie": {
            "uid": "u983e257301e9cc6eb0f2bac49cb"
        },
        "fire4865-yy": {
            "uid": "u39f9e996dc8ca11863b539cadbc7a"
        },
        "huntress": {
            "uid": "ua2ed27b7932f647b492d8ef33c0cc"
        },
        "jerome": {
            "uid": "uf97f2811e2a2a24ad21d4e9e04565"
        },
        "kaida": {
            "uid": "ueaf35f7009d707651e32f7186bac"
        },
        "line": {
            "uid": "u7714db81cdf040de6a11caaab146"
        },
        "mickey": {
            "uid": "ub69bd9eecdaf4e643af552dd13b63"
        },
        "mrnobody": {
            "uid": "u8d322622a9400f2ef437460588ada"
        },
        "naughtyaf": {
            "uid": "u116ef7075f4bf14d0dfc4f0ba4490b"
        },
        "pinkprincess": {
            "uid": "u96f6d4900esdf22812c99bef10d9413aed0"
        },
        "queen": {
            "uid": "ub30557be34sdfdbe8d3a0be4265530f073c"
        },
        "ravenblackmoon": {
            "uid": "u78a3b5sdf1af28b029fdf6e58f886fffcc"
        },
        "sally": {
            "uid": "u3a02b2da2c6f87dsf7d1e45272cb72ce268"
        },
        "smithravi": {
            "uid": "u4d18fe936a783dsf3c36ee8d05f8409d6e6"
        },
        "sugar": {
            "uid": "uf5dedef47529a234a18dc975132d890e4af"
        },
        "twisted": {
            "uid": "u7f62d2a6baf650753234975d063316a1d8c"
        }
    },

` `

You can decompose the dictionary into lists of keys and values: 您可以将字典分解为键和值的列表:

keys, values = mydict.items()

or just work with the keys: 或仅使用按键:

keys = mydict.keys()

Then you can use these to split up your dict: 然后,您可以使用这些命令拆分字典:

dict1 = {k:mydict[k] for k in keys[:19]}
dict2 = {k:mydict[k] for k in keys[19:38]}

etc. 等等

You could do a further list comprehension as follows: 您可以进一步进行列表理解,如下所示:

import math

list_of_subdicts = [{k:mydict[k] for k in keys[19*i:min(len(mydict), 19*(i+1))]} for i in range(math.ceil(len(mydict)/19))]

This will give you a list of dict items, each one with 19 entries except the last one which can have up to 19 entries. 这将为您提供dict项目列表,每个项目有19个条目,但最后一个条目最多可以有19个条目。

You want to split a dictionary into 19 parts? 您想将字典分为19个部分吗? Well, assuming the dictionary is at least 19 entries,you could take the len() of the base dict and divide it by 19. This will be the total number of dictionaries you will need to make. 好吧,假设字典至少有19个条目,则可以将基本字典的len()除以19。这将是您需要制作的字典的总数。 It might work to make a list of dictionaries of that number? 列出该数字的字典可能有用吗? Then just loop through the dictionary and each time your iterator gets to the 19th index, switch to the next dictionary. 然后循环浏览字典,每次迭代器到达第19个索引时,切换到下一个字典。 Hope I understood your question correctly and this is some use to you. 希望我能正确理解您的问题,这对您有所帮助。 I'm not sure, just thinking of the top of my head. 我不确定,只是想到我的头顶。

If you don't care which dictionaries from the original list go into which group of 19, you can just use .keys() to get a list of the original dictionary keys, and add them to a new dictionary-of-dictionaries until each one gets "full" at 19, or until you run out of entries. 如果您不在乎原始列表中的哪些字典进入19个组,则可以使用.keys()获取原始字典键的列表,并将它们添加到新的字典中,直到每个一个在19点“满”,或者直到您用完所有条目。

# Generate a large dictionary of junk
keys = [chr(c) for c in range(40,120) ]
D = dict(zip(keys,[ 'foo' for c in range(len(keys))]))

# This is the dictionary that will have all of the new "sub" dictionaries.
# For keys, just use the integers 0, 1, 2, ... .
new_dicts = { 0: {} }

# Grab each of the original dictionary entries and copy them to
# new dictionaries.  When you've filled up one with 19 characters,
# start filling a new one.  Keep going until you run out of entries.
fill_dict_number = 0
for key in D.keys():
    if len(new_dicts[fill_dict_number]) > 18:
        fill_dict_number += 1
        new_dicts[fill_dict_number] = {}
    new_dicts[fill_dict_number][key] = D[key]

print(new_dicts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM