用python写出给定一个手机号列表,以字典的形式输出四个运营商号码的统计结果
时间: 2024-05-08 09:15:10 浏览: 213
假设手机号列表为phone_list,以下是代码实现:
```python
# 定义四个运营商的号码段
china_mobile = ["134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "182", "183", "184", "187", "188"]
china_unicom = ["130", "131", "132", "155", "156", "185", "186"]
china_telecom = ["133", "153", "180", "181", "189"]
others = ["170", "171", "173", "175", "176", "177", "178", "198", "199"]
# 定义字典统计结果
result = {"China Mobile": 0, "China Unicom": 0, "China Telecom": 0, "Others": 0}
# 统计每个号码的运营商并加入结果字典
for phone in phone_list:
if phone[:3] in china_mobile:
result["China Mobile"] += 1
elif phone[:3] in china_unicom:
result["China Unicom"] += 1
elif phone[:3] in china_telecom:
result["China Telecom"] += 1
else:
result["Others"] += 1
# 输出结果字典
print(result)
```
解释:
首先定义了四个运营商的号码段,然后定义了一个字典result来统计结果,其中四个键分别对应四个运营商,初始值都为0。
然后使用for循环遍历手机号列表,对于每个手机号,根据前三位判断其运营商,然后将对应的结果字典的值加1。
最后输出结果字典。
阅读全文