如何对这个输入字典进行切片和切块,得到想要的输出字典?
How to slice and dice this input dictionary and get the desired output dictionary?
我有一本这样的字典:
d = {
"a": 30,
"b": 45,
"c": 84,
"d": 92,
"e": 93
}
假设我有一个 python 列表,其中包含 93 个项目。前 30 个项目(从索引 0 到索引 30)属于组“a”,从索引 31 到索引 45 的项目属于组“b”。从索引 46 到索引 84 的项目属于组“c”。从索引 85 到索引 92 的项目属于组“d”。第 93 项属于组“e”。
所以上面的字典包含了每组的最后一个索引
从这本字典我想创建一个应该看起来像这样的字典:
final_d = {
"a": [0, 30],
"b": [31, 45],
"c": [46, 84],
"d": [85, 92],
"e": [93]
}
有没有一种优雅的方式来做到这一点?
data = [x for x in range(93)]
d = {
"a": 30,
"b": 45,
"c": 84,
"d": 92,
"e": 93
}
s = 0
final_d = {}
final_data = {}
for index, value in d.items():
e = value
final_d[index] = [s+1,e]
final_data[index] = data[s:e]
s = e
print(final_d)
print(final_data)
输出:
{
'a': [0, 30],
'b': [31, 45],
'c': [46, 84],
'd': [85, 92],
'e': [93, 93]
}
{
'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
'b': [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44],
'c': [45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83],
'd': [84, 85, 86, 87, 88, 89, 90, 91],
'e': [92]
}
一种优雅的方法是使用 pd.qcut
:
import pandas as pd
max_num = d[max(d, key=d.get)]
ls = [v/max_num for _, v in d.items()]
ls.insert(0, 0)
A = pd.qcut(range(max_num + 1),
ls, precision=10 ^ 5).categories.values
dict_ = {A[i] for i, _ in enumerate(d)}
输出:
dict_
{Interval(84.0, 92.0, closed='right'), Interval(-1e-15, 30.0, closed='right'), Interval(92.0, 93.0, closed='right'), Interval(45.0, 84.0, closed='right'), Interval(30.0, 45.0, closed='right')}
拆分成两个列表,键和值,然后将它们以正常顺序和移位顺序放入 zip 函数中。
keys,values=list(d.keys()),list(d.values())
{key:[x+1,y] for key,x,y in zip(keys,[-1]+values[:-1],values)}
输出:
{'a': [0, 30], 'b': [31, 45], 'c': [46, 84], 'd': [85, 92], 'e': [93, 93]}
我有一本这样的字典:
d = {
"a": 30,
"b": 45,
"c": 84,
"d": 92,
"e": 93
}
假设我有一个 python 列表,其中包含 93 个项目。前 30 个项目(从索引 0 到索引 30)属于组“a”,从索引 31 到索引 45 的项目属于组“b”。从索引 46 到索引 84 的项目属于组“c”。从索引 85 到索引 92 的项目属于组“d”。第 93 项属于组“e”。
所以上面的字典包含了每组的最后一个索引 从这本字典我想创建一个应该看起来像这样的字典:
final_d = {
"a": [0, 30],
"b": [31, 45],
"c": [46, 84],
"d": [85, 92],
"e": [93]
}
有没有一种优雅的方式来做到这一点?
data = [x for x in range(93)]
d = {
"a": 30,
"b": 45,
"c": 84,
"d": 92,
"e": 93
}
s = 0
final_d = {}
final_data = {}
for index, value in d.items():
e = value
final_d[index] = [s+1,e]
final_data[index] = data[s:e]
s = e
print(final_d)
print(final_data)
输出:
{
'a': [0, 30],
'b': [31, 45],
'c': [46, 84],
'd': [85, 92],
'e': [93, 93]
}
{
'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
'b': [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44],
'c': [45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83],
'd': [84, 85, 86, 87, 88, 89, 90, 91],
'e': [92]
}
一种优雅的方法是使用 pd.qcut
:
import pandas as pd
max_num = d[max(d, key=d.get)]
ls = [v/max_num for _, v in d.items()]
ls.insert(0, 0)
A = pd.qcut(range(max_num + 1),
ls, precision=10 ^ 5).categories.values
dict_ = {A[i] for i, _ in enumerate(d)}
输出:
dict_
{Interval(84.0, 92.0, closed='right'), Interval(-1e-15, 30.0, closed='right'), Interval(92.0, 93.0, closed='right'), Interval(45.0, 84.0, closed='right'), Interval(30.0, 45.0, closed='right')}
拆分成两个列表,键和值,然后将它们以正常顺序和移位顺序放入 zip 函数中。
keys,values=list(d.keys()),list(d.values())
{key:[x+1,y] for key,x,y in zip(keys,[-1]+values[:-1],values)}
输出:
{'a': [0, 30], 'b': [31, 45], 'c': [46, 84], 'd': [85, 92], 'e': [93, 93]}