On 10/04/2017 05:11 PM, Irv Kalb wrote:
I'm assuming from your posts that you are not a student.  If that is the case, 
look at my solution below.

On Oct 4, 2017, at 9:42 AM, 20/20 Lab <l...@2020fresno.com> wrote:

Looking for advice for what looks to me like clumsy code.

I have a large csv (effectively garbage) dump.  I have to pull out sales 
information per employee and count them by price range. I've got my code 
working, but I'm thinking there must be a more refined way of doing this.

---snippet of what I have---

EMP1 = [0,0]
EMP2 = [0,0]
EMP3 = [0,0]

for line in (inputfile):
     content = line.split(",")
     if content[18] == "EMP1":
         if float(content[24]) < 99.75:
             EMP1[0] += 1
         elif float(content[24]) > 99.74:
             EMP1[1] += 1
     if content[18] == "EMP2":
         if float(content[24]) < 99.75:
             EMP2[0] += 1
         elif float(content[24]) > 99.74:
             EMP2[1] += 1
     if content[18] == "EMP3":
         if float(content[24]) < 99.75:
             EMP3[0] += 1
         elif float(content[24]) > 99.74:
             EMP3[1] += 1

and repeat if statements for the rest of 25+ employees.  I can make a list of the 
employees, but I'd prefer to pull them from the csv, as our turnover is rather high 
(however this is not important).  I'm thinking another "for employee in 
content[18]" should be there, but when I tried, my numbers were incorrect.

Any help / advice is appreciated,

Matt


You could certainly use the csv module if you want, but this builds on your 
start of dealing with the data line by line.

Completely untested, but this approach works by building a dictionary on the 
fly from your data.  Each key is an employee name.  The data associated with 
each key is a two item list of counts.


# Constants
NAME_INDEX = 18
SALES_INDEX = 24
THRESHHOLD = 99.75

salesCountDict = {}  # start with an empty dict

for line in (inputfile):
     content = line.split(",")  # split the line into a list
     name = content[NAME_INDEX]  # extract the name from the content list

     # If we have not seen this employee name before, add it to the dictionary
     # like key value pair:     '<Employee Name>': [0, 0]
     if not(name in employeeDataDict):
         salesCountDict[name] = [0, 0]

     price = float(content[SALES_INDEX])  # extract the price

    # If the price is under some threshhold, increment one value in the 
associated sales list
    # otherwise increment the other
     if price < THRESHHOLD:
         salesCountDict[name][0] += 1
     else:
         salesCountDict[name][1] += 1
# Now you should have a dictionary.  Do what you want with it.  For example:

for name in salesCountDict:
     salesList = salesCountDict[name]
     print(name, salesList)    # Assuming Python 3


Thanks for this.  I've recently had a hard time discerning when to use / the differences of the dict, list, set, tuple.  So this is a huge help.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to