Python 从字典列表中选择带有值条件的随机样本
我有一个字典列表,如下所示:Python 从字典列表中选择带有值条件的随机样本,python,dictionary,Python,Dictionary,我有一个字典列表,如下所示: list_of_dicts = [ {'db': 'redshift', 'table': 'metrics', 'prefix': 'abc_'}, {'db': 'blueshift', 'table': 'colors', 'prefix': 'abc_'}, {'db': 'orangeshift', 'table': 'people', 'prefix': 'def_'}, {'db': 'greenshift', 'tab
list_of_dicts = [
{'db': 'redshift', 'table': 'metrics', 'prefix': 'abc_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'abc_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'def_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'def_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'ghi_'},
{'db': 'brownshift', 'table': 'stages', 'prefix': 'ghi_'},
...
]
如何提取每个前缀的N?例如,假设上面的列表很大,我想得到一个每个前缀5的dict列表。因此,我将得到一个15个dict的列表,5个dict带有abc\uuu
前缀,5个dict带有def\uu
前缀,5个dict带有ghi\uu
预期输出将是:
result = [
{'db': 'redshift', 'table': 'metrics', 'prefix': 'abc_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'abc_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'abc_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'abc_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'abc_'},
{'db': 'redshift', 'table': 'metrics', 'prefix': 'def_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'def_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'def_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'def_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'def_'},
{'db': 'redshift', 'table': 'metrics', 'prefix': 'ghi_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'ghi_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'ghi_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'ghi_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'ghi_'},
]
因此,从一个大的dict列表中随机抽取了每个不同前缀的5个dict。将前缀值作为键的dict分组,并使用defaultdict作为值列出。然后从每个列表中提取n个元素(这里我从每个列表中随机抽取了2个元素),如果需要,使用
itertools.chain
import collections,random, itertools
list_of_dicts = [
{'db': 'redshift', 'table': 'metrics', 'prefix': 'abc_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'abc_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'def_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'def_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'ghi_'},
{'db': 'brownshift', 'table': 'stages', 'prefix': 'ghi_'}
]
d = collections.defaultdict(list)
# group the dicts by prefix
for lst in list_of_dicts:
d[lst["prefix"]].append(lst)
# pick some dicts in each group & flatten the result
# a rare case where the keys aren't important in that step
result = list(itertools.chain.from_iterable(random.sample(v,2) for v in d.values()))
print(result)
一个输出:
[{'db': 'redshift', 'table': 'metrics', 'prefix': 'abc_'},
{'db': 'blueshift', 'table': 'colors', 'prefix': 'abc_'},
{'db': 'greenshift', 'table': 'money', 'prefix': 'def_'},
{'db': 'orangeshift', 'table': 'people', 'prefix': 'def_'},
{'db': 'brownshift', 'table': 'stages', 'prefix': 'ghi_'},
{'db': 'purpleshift', 'table': 'props', 'prefix': 'ghi_'}]