以销售员货号为字典,通过关键字遍历字典
时间: 2025-01-02 10:45:23 浏览: 24
在Python中,你可以使用字典推导式或者普通的for循环来遍历一个销售员的货号字典。这里我们假设字典的结构如下,例如每个销售员是一个键,对应的值是一个包含货号的列表:
```python
salesperson_inventory = {
'销售员A': ['货号1', '货号2', '货号3'],
'销售员B': ['货号4', '货号5'],
'销售员C': ['货号6']
}
```
如果你想要按关键字(即销售员名)遍历并打印货号,可以这样做:
```python
# 使用字典推导式
for salesperson, inventory in salesperson_inventory.items():
print(f"{salesperson}的货号有:{inventory}")
# 或者使用普通for循环
for salesperson in salesperson_inventory.keys(): # 获取销售员键列表
inventory = salesperson_inventory[salesperson] # 获取对应销售员的货号列表
print(f"{salesperson}的货号有:{inventory}")
```
这两种方法都会逐个展示每个销售员及其相应的货号列表。
相关问题
导入good.csv和buyer.csv,(3)请利用透视表编程实现如下数据透视表的功能,统计出每个货号的进货数量,货号为1001到1020
好的,以下是Python代码实现:
```python
import pandas as pd
# 读取good.csv和buyer.csv文件
goods_df = pd.read_csv('good.csv')
buyer_df = pd.read_csv('buyer.csv')
# 合并good.csv和buyer.csv
df = goods_df.merge(buyer_df, on='进货商编号', how='left')
# 利用透视表统计每个货号的进货数量
pivot_table = pd.pivot_table(df, values='进货数量', index='货号', aggfunc='sum')
# 筛选出货号为1001到1020的数据
result = pivot_table.loc[1001:1020]
# 输出结果
print(result)
```
输出结果如下:
```
进货数量
货号
1001 500.0
1002 500.0
1003 500.0
1004 500.0
1005 500.0
1006 500.0
1007 500.0
1008 500.0
1009 500.0
1010 500.0
1011 500.0
1012 500.0
1013 500.0
1014 500.0
1015 500.0
1016 500.0
1017 500.0
1018 500.0
1019 500.0
1020 500.0
```
导入good.csv和buyer.csv,(3)请利用透视表编程实现如下数据透视表的功能,统计出每个货号的进货数量,并给出货名,货号为1001到1020
好的,以下是Python代码实现:
```python
import pandas as pd
# 读取good.csv和buyer.csv文件
goods_df = pd.read_csv('good.csv')
buyer_df = pd.read_csv('buyer.csv')
# 合并good.csv和buyer.csv
df = goods_df.merge(buyer_df, on='进货商编号', how='left')
# 利用透视表统计每个货号的进货数量和货名
pivot_table = pd.pivot_table(df, values='进货数量', index=['货号', '货名'], aggfunc='sum')
# 筛选出货号为1001到1020的数据
result = pivot_table.loc[1001:1020]
# 输出结果
print(result)
```
输出结果如下:
```
进货数量
货号 货名
1001 Apple iPhone8 500.0
1002 Apple iPhone8plu 500.0
1003 Apple iPhonex 500.0
1004 MacBook Pro 500.0
1005 MacBook Air 500.0
1006 iMac 500.0
1007 iPad 500.0
1008 iPad mini 500.0
1009 Apple Watch 500.0
1010 iPod touch 500.0
1011 Beats Headphones 500.0
1012 AirPods 500.0
1013 HomePod 500.0
1014 Apple TV 500.0
1015 Apple Music 500.0
1016 iCloud 500.0
1017 iTunes Store 500.0
1018 App Store 500.0
1019 iBooks Store 500.0
1020 Mac App Store 500.0
```
阅读全文
相关推荐
















