【发布时间】:2022-11-20 00:47:37
【问题描述】:
如果我想获得一个带有 ID 的机器人,它在以下两者之间更快:
storage = {
'bots': [
{ 'id': 123, 'auth': '81792367' },
{ 'id': 345, 'auth': '86908472' },
{ 'id': 543, 'auth': '12343321' }
]
}
id = 345
bot = next(bot['auth'] for bot in storage['bots'] if bot['id'] == id)
和
storage = {
'bots': {
123: '81792367',
345: '86908472',
543: '12343321',
}
}
id = 345
bot = storage['bots'][id]
哪些必须用于 Python pep8 或最漂亮?
【问题讨论】:
-
如果您想知道在您的特定情况下什么更快,那么试试看.如果您有关于风格的问题,那是题外话。
-
使用
timeit模块或 iPython 魔术函数%timeit做一些研究。