Python对姓名列表统计姓名出现次数并输出
时间: 2024-05-06 22:19:20 浏览: 10
可以使用Python的字典(dict)来进行姓名统计。具体实现代码如下:
```
names = ['张三', '李四', '王五', '张三', '赵六', '张三']
name_count = {}
for name in names:
if name not in name_count:
name_count[name] = 1
else:
name_count[name] += 1
for name, count in name_count.items():
print('{}出现了{}次'.format(name, count))
```
输出结果为:
```
张三出现了3次
李四出现了1次
王五出现了1次
赵六出现了1次
```
相关问题
怎么使用python将excle表格进行统计表格中每个姓名的出现次数
可以使用 pandas 库来读取 Excel 表格,并使用 groupby 和 count 方法来统计每个姓名的出现次数。具体实现步骤如下:
1. 安装 pandas 库:在命令行中输入 `pip install pandas`,等待安装完成。
2. 导入 pandas 库:在 Python 脚本中导入 pandas 库,代码如下:
```python
import pandas as pd
```
3. 读取 Excel 表格:使用 pandas 库中的 read_excel 方法读取 Excel 表格,代码如下:
```python
df = pd.read_excel('data.xlsx')
```
其中,data.xlsx 为待读取的 Excel 表格的文件名。
4. 统计出现次数:使用 pandas 库中的 groupby 和 count 方法统计每个姓名的出现次数,代码如下:
```python
counts = df['姓名'].groupby(df['姓名']).count().sort_values(ascending=False)
```
其中,counts 为一个 pandas Series 对象,其中每个元素对应一个姓名及其出现次数。
5. 输出结果:将结果输出为 Excel 表格或者打印出来,代码如下:
```python
counts.to_excel('counts.xlsx', index=True, header=['出现次数'])
```
其中,counts.xlsx 为输出的 Excel 表格的文件名,index=True 表示保留行索引,header=['出现次数'] 表示设置表头为 "出现次数"。
完整的代码示例如下:
```python
import pandas as pd
# 读取 Excel 表格
df = pd.read_excel('data.xlsx')
# 统计出现次数
counts = df['姓名'].groupby(df['姓名']).count().sort_values(ascending=False)
# 输出结果
counts.to_excel('counts.xlsx', index=True, header=['出现次数'])
```
其中,data.xlsx 是一个包含姓名和其他信息的 Excel 表格,'姓名' 是 Excel 表格中的一列,表示每个人的姓名。
python 二维表格有学生学号,姓名和性别 分性别统计姓出现的次数
假设我们有一个二维表格,名为 student_table,其中包含学生的学号、姓名和性别。可以按以下步骤分性别统计姓出现的次数:
1. 首先,我们可以定义一个字典变量,名为 name_count,用于存储计数结果。字典的键是姓,值是该姓在学生姓名中出现的次数。例如,name_count['张'] = 5 表示有 5 位学生姓张。
```python
name_count = {}
```
2. 接着,我们遍历 student_table 中的每一行记录,对每一位学生进行处理。
```python
for row in student_table:
# 处理每个学生的姓名
name = row[1]
# 处理学生姓名中的姓
surname = name[0]
if surname in name_count:
name_count[surname] += 1
else:
name_count[surname] = 1
```
3. 对于性别分别计数,我们需要在处理每个学生前,判断该学生的性别是否在本次统计中需要考虑。例如,只统计女生姓名中姓张出现的次数,可以在处理每位女生之前,先判断该女生的性别是否为女性。
```python
for row in student_table:
# 处理每个学生的性别
gender = row[2]
# 判断该学生的性别是否需要考虑
if gender == '女':
# 处理每个学生的姓名
name = row[1]
# 处理学生姓名中的姓
surname = name[0]
if surname in name_count:
name_count[surname] += 1
else:
name_count[surname] = 1
```
4. 最后,我们可以将统计结果按姓的字母顺序输出,便于查看和比较。
```python
# 按姓的字母顺序排序
sorted_names = sorted(name_count.keys())
for name in sorted_names:
print(name, name_count[name])
```
完整代码如下:
```python
# 定义一个字典,用于存储计数结果
name_count = {}
# 遍历 student_table 中的每一行记录
for row in student_table:
# 处理每个学生的性别
gender = row[2]
# 判断该学生的性别是否需要考虑
if gender == '女':
# 处理每个学生的姓名
name = row[1]
# 处理学生姓名中的姓
surname = name[0]
if surname in name_count:
name_count[surname] += 1
else:
name_count[surname] = 1
# 按姓的字母顺序排序
sorted_names = sorted(name_count.keys())
# 输出统计结果
for name in sorted_names:
print(name, name_count[name])
```
注意:以上代码只是一个示例,实际应用中可能需要根据具体的数据结构和数据格式进行修改。
相关推荐
![](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)