Python - Load Transaction Data Into A List Of Lists, Count Occurrence Of Each String
I'm getting back into python on account of having a homework assignment in it, and am looking for assistance on speeding up sections of my code. My last post was downvoted on accou
Solution 1:
You encountered the sadly classical test on dictionary keys instead of in the dictionary itself.
if item in first_lookup.keys():
should be
if item in first_lookup:
to benefit from dictionary lookup. Explicit call to first_lookup.keys()
generates a list
in Python 2, so in
applies to a list not a dictionary.
In your case, replacing that loop:
forlinein item_data_lol:
line = line[0]
foritemin line.split():
if item in first_lookup.keys():
first_lookup[item] += 1else:
first_lookup[item] = 1
by this would speed it up even more (using collections.Counter
initialized by a generator comprehension):
importcollectionsfirst_lookup= collections.Counter(item for line in item_data_lol for item in line[0].split())
Post a Comment for "Python - Load Transaction Data Into A List Of Lists, Count Occurrence Of Each String"