如何最小化Python中的循环执行时间?
我正在尝试使用 for 循环执行此代码,
for i in fielding: # fielding has nearly 19000 records - dtype (list of dictionary)
for j in player: # player has nearly 17000 records - dtype (list of dictionary)
if i['id'] == j['id']:
j['name'] = 'Luke'
所以运行需要更长的时间。有没有其他方法可以实现这一目标?谢谢你的帮助。
回答
看起来您正在尝试通过标识符(或任何其他字段)连接数据。这就是您可以使用dict或set加速 id-lookups 的情况:
# Build a set of ids you need to update
fielding_ids = {f['id'] for f in fielding}
# Or use dict if you will need to access the records
# fielding_ids = {f['id']: f for f in fielding}
# Update records with corresponding ids from the second list
for p in player:
if p['id'] in fielding_ids:
p['name'] = 'Luke'
Pythonset()并且dict()是基于 hashmap 的容器,它们针对几乎即时的密钥存在检查进行了优化。