list筛选数据
对list筛选数据,隐藏掉 'apiGroup': 'api',和'ID': 18的数据
实现如下:
筛选后:
{'ID': 17, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/base/login', 'description': '用户登录(必选)', 'apiGroup': 'base', 'method': 'POST'}
{'ID': 19, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/casbin/getPolicyPathByAuthorityId', 'description': '获取权限列表', 'apiGroup': 'casbin', 'method': 'POST'}
筛选前:----------------------------
{'ID': 12, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/createApi', 'description': '创建api', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 13, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/deleteApi', 'description': '删除Api', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 14, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/updateApi', 'description': '更新Api', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 15, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/getApiList', 'description': '获取api列表', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 16, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/getAllApis', 'description': '获取所有api', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 17, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/getApiById', 'description': '获取api详细信息', 'apiGroup': 'api', 'method': 'POST'}
{'ID': 18, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/api/deleteApisByIds', 'description': '批量删除api', 'apiGroup': 'api', 'method': 'DELETE'}
{'ID': 17, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/base/login', 'description': '用户登录(必选)', 'apiGroup': 'base', 'method': 'POST'}
{'ID': 18, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/casbin/updateCasbin', 'description': '更改角色api权限', 'apiGroup': 'casbin', 'method': 'POST'}
{'ID': 19, 'CreatedAt': '2021-12-25T12:51:57.127+08:00', 'UpdatedAt': '2021-12-25T12:51:57.127+08:00', 'path': '/casbin/getPolicyPathByAuthorityId', 'description': '获取权限列表', 'apiGroup': 'casbin', 'method': 'POST'}
s = {
"code": 0,
"data": {
"list": [{
"ID": 12,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/createApi",
"description": "创建api",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 13,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/deleteApi",
"description": "删除Api",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 14,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/updateApi",
"description": "更新Api",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 15,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/getApiList",
"description": "获取api列表",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 16,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/getAllApis",
"description": "获取所有api",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 17,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/getApiById",
"description": "获取api详细信息",
"apiGroup": "api",
"method": "POST"
}, {
"ID": 18,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/api/deleteApisByIds",
"description": "批量删除api",
"apiGroup": "api",
"method": "DELETE"
}, {
"ID": 17,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/base/login",
"description": "用户登录(必选)",
"apiGroup": "base",
"method": "POST"
}, {
"ID": 18,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/casbin/updateCasbin",
"description": "更改角色api权限",
"apiGroup": "casbin",
"method": "POST"
}, {
"ID": 19,
"CreatedAt": "2021-12-25T12:51:57.127+08:00",
"UpdatedAt": "2021-12-25T12:51:57.127+08:00",
"path": "/casbin/getPolicyPathByAuthorityId",
"description": "获取权限列表",
"apiGroup": "casbin",
"method": "POST"
}],
"total": 79,
"page": 1,
"pageSize": 10
},
"msg": "获取成功"
}
tmplist1 =s['data']['list']
# print(tmplist1)
# print(type(tmplist1))
def list_screen(tmplist):
## list2 获取tmplist要筛选掉的下标,遍历tmplist不包含list2下标的数据装到tmplist_screen,并返回
list2=[]
tmplist_screen=[]
for x in range (len(tmplist)):
##筛选条件
if tmplist[x]['apiGroup']=='api' or tmplist[x]['ID']==18:
list2.append(x)
for x in range (len(tmplist)):
if x not in list2:
tmplist_screen.append(tmplist[x])
return tmplist_screen
for y in list_screen(tmplist1):
print(y)
print('----------------------------')
for y in tmplist1:
print(y)
参考文章: Python 列表(List) 的三种遍历(序号和值)方法
python-----删除列表中某个元素的3种方法