python 二维表格有姓名性别 分性别统计姓
时间: 2023-05-25 19:07:03 浏览: 170
这里提供一种实现方式:
```python
# 二维表格数据
data = [
["张三", "男"],
["李四", "男"],
["王五", "女"],
["赵六", "女"],
["钱七", "男"],
["孙八", "女"],
["周九", "男"],
["吴十", "女"],
]
# 姓氏分类
male_names = [] # 男性姓名列表
female_names = [] # 女性姓名列表
for row in data:
name, gender = row
if gender == "男":
male_names.append(name)
elif gender == "女":
female_names.append(name)
# 统计姓氏数量
def count_surname(names):
surname_count = {}
for name in names:
surname = name[0]
if surname in surname_count:
surname_count[surname] += 1
else:
surname_count[surname] = 1
return surname_count
male_surname_count = count_surname(male_names) # 男性姓氏数量
female_surname_count = count_surname(female_names) # 女性姓氏数量
# 打印结果
print("男性姓氏:")
for surname, count in male_surname_count.items():
print(surname, count)
print("女性姓氏:")
for surname, count in female_surname_count.items():
print(surname, count)
```
输出结果:
```
男性姓氏:
张 1
李 1
钱 1
周 1
女性姓氏:
王 1
赵 1
孙 1
吴 1
```
阅读全文