所以我想将字典拆分为19个部分,这仅是一件事,因此它不会保留或保存,它只是程序的一部分,因此将其拆分为几个部分19个,所以如果有28个,它将分成两部分,一个19另一个9。
如果这根本没有道理,那么您就只能为该xD做斗争 例如:
1-
"69angel": {"uid": "u99bd5055900298f13821ad6"
},
"bestbrittany": {
"uid": "u80d4520e66090a088a8e73b2af"
},
2-
"blegh": {
"uid": "u0c4afad0a1e1d3b9ffdd3db444d"
},
"cassie84": {
"uid": "u9b53c15bfd2f0e5a3741be1297"
},
等等等
如果您想将下面的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"
}
},
`
答案 0 :(得分:2)
您可以将字典分解为键和值的列表:
keys, values = mydict.items()
或仅使用按键:
keys = mydict.keys()
然后,您可以使用它们来分割字典:
dict1 = {k:mydict[k] for k in keys[:19]}
dict2 = {k:mydict[k] for k in keys[19:38]}
等
您可以进一步进行列表理解,如下所示:
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))]
这将为您提供dict
个项目的列表,每个项目都有19个条目,最后一个条目最多可以有19个条目。
答案 1 :(得分:0)
您想将字典分为19个部分吗?好吧,假设字典至少有19个条目,则可以将基本字典的len()除以19。这将是您需要制作的字典的总数。列出该数字的字典可能有用吗? 然后循环浏览字典,每次迭代器到达第19个索引时,切换到下一个字典。希望我能正确理解您的问题,这对您有所帮助。我不确定,只是想到我的头顶。
答案 2 :(得分:0)
如果您不在乎原始列表中的哪些词典进入哪个19组,则可以使用.keys()
获取原始字典键的列表,并将其添加到新的字典中。 -dictionaries,直到每个人都在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)