从字典概率明智的挑选
假设我
{'us':
{'male':
{'given_names':
['Alex', 'Bob', 'Charlie']
},
'female':
{'given_names':
['Alice', 'Betty', 'Claire']
}
},
'uk':
{'male':
{'given_names':
['aaa', 'Bbb', 'cc']
},
'female':
{'given_names':
['ppp', 'ddd', 'sss']
}
}
}
现在有一个词典,假设我想获得60%的美国名字,40%的英国名字,但有50%的男性和女性的名字。
我该怎么做?
当前的方法?试图思考类似于 但是我想这比那更复杂。
我当时想先获取所有名称,然后从中应用分发?但这并不是一定的逻辑意义。有人可以帮忙吗?
# all_possible_names = [
# name
# for list_of_names in [
# self.library[area][gender][
# "given_names"
# ]
# for gender in self.genders
# for area in self.name_areas
# ]
# for name in list_of_names
# ]
# print(all_possible_names) `
谢谢。
Let's say I have a dictionary
{'us':
{'male':
{'given_names':
['Alex', 'Bob', 'Charlie']
},
'female':
{'given_names':
['Alice', 'Betty', 'Claire']
}
},
'uk':
{'male':
{'given_names':
['aaa', 'Bbb', 'cc']
},
'female':
{'given_names':
['ppp', 'ddd', 'sss']
}
}
}
Now let's say I want to get 60% US names, 40% UK names, but with 50 50 % males and females names.
How Can I do it?
Current approach? Trying to think something similar to this
But I guess it is more complex then that.
I was thinking to get all the names first, then applying a distribution from them? But it is not making some logical sense. Can someone help?
# all_possible_names = [
# name
# for list_of_names in [
# self.library[area][gender][
# "given_names"
# ]
# for gender in self.genders
# for area in self.name_areas
# ]
# for name in list_of_names
# ]
# print(all_possible_names) `
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
andy.Choices
带重量,选择
在男性/女性之间分开,假设您的字典被命名为d
和n 是您想要的名称总量,然后:
Use
random.choices
with a weight andchoice
to split between male/female, assuming your dictionary is namedd
andN
is the total amount of names you'd like, then:您可以使用numpy的随机。选择进行重量分布,
我在上面添加了一些逻辑并检查分布。
它可以缩短为以下逻辑:
You can use numpy's random.choice to do the weight distribution
I added some logic above for my testing, and to check the distribution.
It can be shortened to the logic below: