用python编写字典
时间: 2024-01-24 20:13:21 浏览: 26
下面是两种用Python编写字典的方法:
1. 直接定义字典变量,使用花括号{}将键值对括起来,每个键值对之间用逗号隔开。例如:
```python
favorite_languages = {'jen': 'python', 'sarah': 'C', 'edward': 'ruby', 'phil': 'python'}
```
2. 使用dict()函数创建字典,将键值对作为参数传入。例如:
```python
favorite_languages = dict(jen='python', sarah='C', edward='ruby', phil='python')
```
注意:在第二种方法中,键名不需要加引号。
相关问题
用python编写字典之手机号归属运营商判断
可以使用Python模块phonenumbers来判断手机号的归属运营商。该模块可以解析国际手机号,并提供国际区号、国家名称、手机号类型、运营商信息等。
首先,需要安装phonenumbers模块,可以使用pip命令进行安装:
```
pip install phonenumbers
```
然后,可以使用以下代码实现手机号归属运营商判断:
```python
import phonenumbers
def get_phone_info(phone_number):
phone_info = {}
parsed_number = phonenumbers.parse(phone_number)
phone_info['country'] = phonenumbers.region_code_for_number(parsed_number)
phone_info['type'] = phonenumbers.number_type(parsed_number)
carrier_en = phonenumbers.carrier.name_for_number(parsed_number, 'en')
carrier_zh = phonenumbers.carrier.name_for_number(parsed_number, 'zh_CN')
phone_info['carrier'] = carrier_en if carrier_en != 'Unknown' else carrier_zh
return phone_info
```
在上述代码中,get_phone_info()函数接收一个手机号作为参数,返回一个包含国家代码、手机号类型、运营商信息的字典。通过phonenumbers.parse()函数解析手机号,然后使用phonenumbers.region_code_for_number()函数获取国家代码,使用phonenumbers.number_type()函数获取手机号类型,使用phonenumbers.carrier.name_for_number()函数获取运营商信息。
以下是一个使用示例:
```python
phone_number = '+8613812345678'
phone_info = get_phone_info(phone_number)
print(phone_info)
```
输出结果如下:
```
{'country': 'CN', 'type': 1, 'carrier': 'China Telecom'}
```
其中,'type': 1表示该号码是移动电话。'carrier': 'China Telecom'表示该号码属于中国电信运营商。
python用字典编写超市收银系统
好的,Python中可以用字典来编写超市收银系统。我们可以将每个商品的信息(如名称、价格、数量等)存储在一个字典中,然后将所有的商品字典存储在一个列表中。当顾客购买商品时,我们可以从列表中找到相应的商品字典,计算出总价并输出。
以下是一个简单的示例代码:
```
# 商品列表
items = [
{"name": "牛奶", "price": 10, "quantity": 5},
{"name": "面包", "price": 5, "quantity": 10},
{"name": "啤酒", "price": 20, "quantity": 8},
{"name": "薯片", "price": 8, "quantity": 12},
]
# 计算总价
total_price = 0
for item in items:
print("商品名称:{},单价:{},数量:{}".format(item["name"], item["price"], item["quantity"]))
total_price += item["price"] * item["quantity"]
print("总价为:{}".format(total_price))
```
以上代码中,我们首先定义了一个商品列表items,其中每个元素都是一个商品字典。然后,我们遍历商品列表,输出每个商品的信息,并累加每个商品的总价。最后输出总价。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)