我有以下代码#1:
await cur.execute(query)
async for row in cur:
if row[1] not in banned_doorbots:
result.append({
'ding_id': row[0],
'doorbot_id': row[1],
'kind': row[2]
})
return result
我把它重构成#2:
await cur.execute(query)
keys = ('ding_id', 'doorbot_id', 'kind')
return [dict(zip(keys, row)) async for row in cur
if row[1] not in banned_doorbots]
但是现在我有一个问题,我的ding_id
应该包含str
类型,
就像这样'ding_id': str(row[0])
如何使用我的#2解决方案?你知道吗
^{} 不关心类型,当然也不会将整数转换为字符串。唯一重要的是,参数应该是iterables(在您的示例中似乎就是这样)。不过,这些iterables中的元素没有受到影响。你知道吗
您需要提供
row
和cur
的具体示例,但我真的认为zip
不是问题所在。你知道吗如果您有一个整数列表并希望将其转换为字符串,则可以使用^{} :
这应该起作用:
如果
row
是元组,请首先转换为列表:相关问题 更多 >
编程相关推荐