开发者

List comprehension on Python dict doesn't perform well

开发者 https://www.devze.com 2023-02-15 19:24 出处:网络
I have the following loop: for row in rows: print row.value(\'customer\', \'name\') #print [customer for customer in customers if customer[\'name\'] == row.value(\'customer\', \'name\')]

I have the following loop:

for row in rows:
    print row.value('customer', 'name')
#   print [customer for customer in customers if customer['name'] == row.value('customer', 'name')]

When I uncomment the second print, the loop runs much more slowly. Is there a way I can write this to make it perform faster?

My overall problem is th开发者_JAVA技巧at I used to do a separate SQL query for each row to get the matching customer for that row. Now I'm trying to grab all the customers up front in an effort to improve performance.

One row of customers looks like this, FYI:

{'name': u'JOHN SMITH', 'created_at': datetime.datetime(2010, 12, 1, 14, 49, 57), 'updated_at': datetime.datetime(2011, 3, 3, 16, 41, 1), 'customer_number': u'C102340', 'phone': u'', 'social_security_number': u'2352352', 'do_not_mail': None, 'id': 4154L, 'deceased': None}


"It runs much more slowly" because you are looking at the entire customer-list for each row.

Either make a dictionary of customers and use a lookup:

customerIndex = { customer['name']:customer for customer in customers }
for row in rows:
    name = row.value('customer','name')
    print name, customerIndex[name]

or modify your SQL query to return relevant customer information with each row (something like row INNER JOIN customer ON row.customer_id==customer.id)


  1. It's not clear what exactly 'customers' is. If you make a dict from name to row and say customers.get(row.value('customer', 'name')) it should do a very fast lookup for each customer.
  2. Don't store social security numbers unless you absolutely have to. I know this is unrelated, but it is really easy to take the last 4-5 digits of a SSN and recover the full SSN if you know where the person lives and how old they are.
0

精彩评论

暂无评论...
验证码 换一张
取 消