python 2.2.18 遍历某字段,如果遍历字段的前2-3个字符和数组中的字段对的上,则字段xx(信息科技部)变更为xx(大数据部门)
时间: 2024-05-06 11:18:20 浏览: 63
假设你有一个包含字段的列表,可以按照以下步骤进行处理:
1. 定义一个字典来存储对应关系:
```
dept_dict = {
'xx(信息科技部)': 'xx(大数据部门)',
# 其他需要替换的字段和对应值
}
```
2. 遍历列表中的每个字段,判断前2-3个字符是否匹配字典中的某个键:
```
for i in range(len(field_list)):
if field_list[i][:3] in dept_dict:
```
3. 如果匹配成功,则将该字段替换为对应的值:
```
field_list[i] = dept_dict[field_list[i][:3]]
```
完整代码如下:
```
dept_dict = {
'xx(信息科技部)': 'xx(大数据部门)',
# 其他需要替换的字段和对应值
}
for i in range(len(field_list)):
if field_list[i][:3] in dept_dict:
field_list[i] = dept_dict[field_list[i][:3]]
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![whl](https://img-home.csdnimg.cn/images/20250102104920.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)