假设员工的姓均为姓名中的第1个字符。编写程序,统计下列列表中各姓氏所含的人数。 测试示例如下: 列表如下: incomeList = [ ['10932','张珊',15000.00 ,4000.00 ,2300.00 ,980.00 ,300.00 ,1000.00], ['10933','李思',12000.00 ,5000.00 ,2600.00 ,890.00 ,400.00 ,2000.00], ['10934','王武',10100.00 ,2000.00 ,2500.00 ,860.00 ,500.00 ,1500.00], ['10935','赵柳',10050.00 ,1000.00 ,2700.00 ,1080.00 ,300.00 ,2000.00], ['10936','钱棋',10200.00 ,2000.00 ,2400.00 ,1000.00 ,600.00 ,1000.00], ['10941','张明',13600.00 ,4000.00 ,2100.00 ,680.00 ,300.00 ,1000.00], ['10942','赵敏',13200.00 ,2500.00 ,2300.00 ,670.00 ,400.00 ,3000.00], ['10945','王红',13600.00 ,2600.00 ,2030.00 ,560.00 ,400.00 ,2000.00], ['10946','李萧',12500.00 ,2800.00 ,2400.00 ,420.00 ,500.00 ,1500.00], ['10947','孙科',12000.00 ,3500.00 ,2030.00 ,880.00 ,300.00 ,1000.00], ['10948','刘利',14200.00 ,2500.00 ,2020.00 ,900.00 ,400.00 ,2000.00] ]
时间: 2023-06-24 21:04:04 浏览: 323
Python统计一个字符串中每个字符出现了多少次的方法【字符串转换为列表再统计】
你可以使用 Python 的字典 (dictionary) 来实现这个统计。具体思路是,遍历列表中每个员工的姓名,取出姓氏第一个字符,然后检查字典中是否已经有这个姓氏。如果有,将对应的计数器加1;如果没有,将这个姓氏作为键,计数器初始化为1,加入字典中。
下面是实现代码:
```python
incomeList = [
['10932','张珊',15000.00 ,4000.00 ,2300.00 ,980.00 ,300.00 ,1000.00],
['10933','李思',12000.00 ,5000.00 ,2600.00 ,890.00 ,400.00 ,2000.00],
['10934','王武',10100.00 ,2000.00 ,2500.00 ,860.00 ,500.00 ,1500.00],
['10935','赵柳',10050.00 ,1000.00 ,2700.00 ,1080.00 ,300.00 ,2000.00],
['10936','钱棋',10200.00 ,2000.00 ,2400.00 ,1000.00 ,600.00 ,1000.00],
['10941','张明',13600.00 ,4000.00 ,2100.00 ,680.00 ,300.00 ,1000.00],
['10942','赵敏',13200.00 ,2500.00 ,2300.00 ,670.00 ,400.00 ,3000.00],
['10945','王红',13600.00 ,2600.00 ,2030.00 ,560.00 ,400.00 ,2000.00],
['10946','李萧',12500.00 ,2800.00 ,2400.00 ,420.00 ,500.00 ,1500.00],
['10947','孙科',12000.00 ,3500.00 ,2030.00 ,880.00 ,300.00 ,1000.00],
['10948','刘利',14200.00 ,2500.00 ,2020.00 ,900.00 ,400.00 ,2000.00]
]
count_dict = {}
for employee in incomeList:
first_char = employee[1][0]
if first_char in count_dict:
count_dict[first_char] += 1
else:
count_dict[first_char] = 1
print(count_dict)
```
输出结果为:
```
{'张': 2, '李': 2, '王': 2, '赵': 2, '钱': 1, '孙': 1, '刘': 1}
```
这说明,姓张、李、王、赵各有2人,姓钱、孙、刘各有1人。
阅读全文