[python]List 过滤

获取数据库列表屏蔽系统自带数据库

# 原代码
db_list_result = [(‘master‘, ), (‘tempdb‘, ), (‘model‘, ), (‘msdb‘, ), (‘student‘, ), (‘school‘, )]
db_list = []
for _database in db_list_result:
    if ‘master‘ in _database:
        continue
    if ‘tempdb‘ in _database:
        continue
    if ‘model‘ in _database:
        continue
    if ‘msdb‘ in _database:
        continue
    db_list.append(_database[0])
print(db_list)

# 修改后代码
result2 = [db_name[0] for db_name in db_list_result if not (db_name[0] in [‘master‘,‘tempdb‘,‘model‘,‘msdb‘])]
print(result2)
#----输出结果------
[‘student‘, ‘school‘]

相关推荐