我使用python3.6。 我已使用Pyhive连接到配置单元,并且我的光标是cur。你知道吗
目标: 在字典中构建数据库-表映射: dbname1:此数据库中的表列表。你知道吗
我已经建立了它使用for循环,但我想把它转移到字典理解请告诉我什么是我的dict理解错误:
lst = extract_dbs_tbls(cur, s = 'show databases')
map_tbl_db = {}
for i in lst:
cur.execute("use %s" % i)
map_tbl_db['i'] = extract_dbs_tbls(cur, 'show tables')
注意:lst是数据库名称的列表,如['default', 'dwh', ...]
基本上我想要的是以下的听写理解:
{i:j for i in lst cur.execute('use %i' % i) j = extract_dbs_tbls(cur, s = 'show tables')}
其中提取数据库:
def extract_dbs_tbls(cur, s = 'show tables'):
"Return list of dbs / tables. Note: use before 'use db_name'."
cur.execute(s)
lst = cur.fetchall()
return [j[0] for j in lst];
要调用
cur.execute
作为列表理解中的附加操作,您可以使用一些伪var和list(cur.execute
放在list[ ]
中创建iterable,其中一个元素只是用来调用包含的函数),这些变量不会在result dict中使用。并且您的j
var不需要,而是由就地调用extract_dbs_tbls
替换:以上相当于您的循环:
相关问题 更多 >
编程相关推荐