You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化Python列表搜索速度(解决账号匹配循环效率问题)

Optimizing Your Account Matching & Sum Calculation

Ah, I’ve dealt with this exact performance bottleneck before—your current code is slow because of one critical issue: checking membership in a list (row[1] in Accounts) is an O(n) operation. With 10,000 rows and 100,000 accounts, that’s 1 billion individual checks under the hood. Yikes!

Here are three proven ways to speed this up, starting with the quickest fix:

1. Convert Accounts to a Set (Instant Speed Boost)

The simplest and most impactful change is turning your Accounts list into a Python set. Sets use hash tables for membership checks, which drop the time complexity from O(n) to O(1). This alone will make your code run hundreds of times faster.

# Convert Accounts list to a set first
accounts_set = set(Accounts)

calc = 0
for row in listOfLists:
    if row[1] in accounts_set:
        calc += row[8]

2. Use a Generator Expression for Conciseness (Minor Extra Speed)

Once you’ve switched to a set, you can streamline the code with a generator expression. This eliminates the explicit loop variable and leverages Python’s optimized internal summation logic, giving a small additional speedup while making the code cleaner.

accounts_set = set(Accounts)
calc = sum(row[8] for row in listOfLists if row[1] in accounts_set)

3. Go Vectorized with Pandas (For Large-Scale Workflows)

If you’re working with even bigger datasets down the line, or want to integrate this into a more robust data pipeline, using pandas is the way to go. Pandas uses optimized C-backed operations (vectorization) that avoid Python-level loops entirely, which can be orders of magnitude faster for large data.

import pandas as pd

# Convert your list of lists to a DataFrame
df = pd.DataFrame(listOfLists)

# Use set for fast lookups, then filter and sum
accounts_set = set(Accounts)
calc = df[df[1].isin(accounts_set)][8].sum()

Quick Note on Why This Works:

Lists are ordered and require linear scanning to check membership, but sets are unordered and use hash values to jump directly to the item you’re looking for. This is the single biggest win here—all other optimizations build on this foundation.

内容的提问来源于stack exchange,提问作者Henrik Poulsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:00