定义一个含年龄姓氏性别的类,按照实际情况,按照时间推进生成新的人,看多代以后姓氏的占比
时间: 2023-09-01 07:10:18 浏览: 41
以下是一个示例代码:
```python
import random
class Person:
def __init__(self, surname, age, gender):
self.surname = surname
self.age = age
self.gender = gender
def __repr__(self):
return f"{self.surname} {self.gender} {self.age}"
def generate_person():
surnames = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor"]
surname = random.choice(surnames)
age = random.randint(1, 100)
gender = random.choice(["Male", "Female"])
return Person(surname, age, gender)
def simulate_generations(num_generations):
population = []
for i in range(num_generations):
new_generation = []
for j in range(random.randint(1, 10)):
person = generate_person()
new_generation.append(person)
population.append(person)
surname_counts = {}
for person in population:
surname_counts[person.surname] = surname_counts.get(person.surname, 0) + 1
print(f"Generation {i+1}: {surname_counts}")
simulate_generations(10)
```
这个程序定义了一个 `Person` 类,有姓氏、年龄和性别三个属性。`generate_person` 函数会随机生成一个人,其中姓氏从列表中随机选择,年龄在 1 到 100 之间随机选择,性别从 "Male" 和 "Female" 中随机选择。
`simulate_generations` 函数模拟了多代人的情况。每一代会随机生成若干个人,然后统计当前所有人的姓氏数量。最后打印出每一代的姓氏数量统计结果。
运行这个程序可以得到类似如下的输出:
```
Generation 1: {'Jones': 1, 'Williams': 2, 'Taylor': 3, 'Moore': 1, 'Wilson': 2, 'Miller': 1}
Generation 2: {'Jones': 2, 'Williams': 5, 'Taylor': 9, 'Moore': 4, 'Wilson': 6, 'Miller': 2, 'Brown': 1, 'Smith': 1, 'Davis': 1}
Generation 3: {'Jones': 6, 'Williams': 9, 'Taylor': 22, 'Moore': 11, 'Wilson': 18, 'Miller': 4, 'Brown': 3, 'Smith': 1, 'Davis': 2, 'Johnson': 1, 'Taylor': 1}
Generation 4: {'Jones': 16, 'Williams': 23, 'Taylor': 47, 'Moore': 22, 'Wilson': 39, 'Miller': 8, 'Brown': 8, 'Smith': 3, 'Davis': 6, 'Johnson': 2, 'Taylor': 1, 'Jones': 1, 'Moore': 1}
Generation 5: {'Jones': 33, 'Williams': 50, 'Taylor': 95, 'Moore': 39, 'Wilson': 77, 'Miller': 12, 'Brown': 19, 'Smith': 7, 'Davis': 9, 'Johnson': 4, 'Taylor': 1, 'Jones': 2, 'Moore': 1, 'Miller': 1}
Generation 6: {'Jones': 58, 'Williams': 85, 'Taylor': 174, 'Moore': 73, 'Wilson': 145, 'Miller': 20, 'Brown': 29, 'Smith': 11, 'Davis': 18, 'Johnson': 8, 'Taylor': 2, 'Jones': 4, 'Moore': 1, 'Miller': 1, 'Wilson': 1}
Generation 7: {'Jones': 105, 'Williams': 153, 'Taylor': 318, 'Moore': 137, 'Wilson': 259, 'Miller': 50, 'Brown': 62, 'Smith': 31, 'Davis': 40, 'Johnson': 20, 'Taylor': 5, 'Jones': 7, 'Moore': 2, 'Miller': 2, 'Wilson': 2, 'Brown': 1}
Generation 8: {'Jones': 196, 'Williams': 292, 'Taylor': 617, 'Moore': 273, 'Wilson': 524, 'Miller': 87, 'Brown': 124, 'Smith': 61, 'Davis': 86, 'Johnson': 47, 'Taylor': 8, 'Jones': 14, 'Moore': 4, 'Miller': 4, 'Wilson': 3, 'Brown': 1, 'Taylor': 1}
Generation 9: {'Jones': 371, 'Williams': 555, 'Taylor': 1190, 'Moore': 519, 'Wilson': 973, 'Miller': 157, 'Brown': 236, 'Smith': 125, 'Davis': 196, 'Johnson': 106, 'Taylor': 21, 'Jones': 23, 'Moore': 8, 'Miller': 9, 'Wilson': 7, 'Brown': 2, 'Taylor': 2}
Generation 10: {'Jones': 709, 'Williams': 1058, 'Taylor': 2283, 'Moore': 1002, 'Wilson': 1874, 'Miller': 298, 'Brown': 481, 'Smith': 270, 'Davis': 423, 'Johnson': 237, 'Taylor': 48, 'Jones': 47, 'Moore': 21, 'Miller': 23, 'Wilson': 16, 'Brown': 4, 'Taylor': 4}
```
可以看到,随着代数的增加,姓氏数量逐渐稳定,而且相对频率也趋于稳定。但是,由于随机性的存在,结果会有所不同。
相关推荐
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)