Home > Mobile >  how to add together the items in a list after a certain condition is true?
how to add together the items in a list after a certain condition is true?

Time:06-10

I am trying to write a value return function in which the parameter is a list of records that show store sales and number of employees and such.

The function iterates through the record list and calculates the total number

of employees from stores with sales volume more than $50,000.

the "records" parameter is an txt file that contains the information in the following format: Hm001,6,Frankton,42305.67 id, num of employees, suburb, sales count

I have written a if statement for the stores that earn more than 50000. I need help with showing the total number of employees from stores that have sales return of more than 50000.

the solution must be general and work for any list with the same format. Please explain your answer as I am new to python.


def count_employees(records):
   num_of_emp = ""
   for count in records: 
      if count[3] > 50000: 
         num_of_emp = 
   return count   
          ```

**EDIT** this is the sample records: 

Hm001,6,Frankton,42305.67 Hm002,10,Glenview,21922.22 Hm003,7,Silverdale,63277.9 Hm004,13,Glenview,83290.09 Hm005,21,Queenwood,81301.82 Hm006,14,Hillcrest,62333.3 Hm007,7,Frankton,28998.8 Hm008,19,Chartwell,51083.5 Hm009,6,Glenview,62155.72 Hm0010,8,Enderley,33075.1 Hm0011,10,Fairfield,61824.7 Hm0012,15,Rototuna,21804.8 Hm0013,11,Fairfield,62804.7

CodePudding user response:

The input data is comprised of records such as:

Hm001,6,Frankton,42305.67

Each record is comprised of 4 components separated by comma.

There are multiple records in the input data (per line) separated by whitespace.

FILENAME = 'foo.txt'
VOLUME = 50_000

def emp_count(filename):
    count = 0
    with open(filename) as data:
        for line in map(str.strip, data):
            for store in line.split():
                _, emps, _, sales = store.split(',')
                if float(sales) > VOLUME:
                    count  = int(emps)
    return count


print(emp_count(FILENAME))

Output: (based on the sample data shown in the question)

101

Note:

This handles any number of records per line in the input file providing the format is as shown in the question

CodePudding user response:

Something like this should work. Don't tested it tho:

file = "text.txt"

def count_employees(filename):
    num_of_emp = 0
    with open(filename) as f:
        all_data = f.readlines()
        lines = all_data[0].split(" ")
        for line in lines:
            record = line.split(",")
            if float(record[3]) > 50000:
                num_of_emp  = int(record[1])
    return num_of_emp

print(count_employees(file))


CodePudding user response:

What exactly are you having an issue with? Can you be more specific in what you don't understand? Your problem can be easily researched

You have initialised num_of_emp incorrectly:

num_of_emp = "" # this should not be an empty string

You are also returning the incorrect variable as 'count' only exists within the for loop.

CodePudding user response:

Ok, so for adding a new element to the list, you can make use of .append() it adds a new element to the list.

so the syntax goes like:

list_name.append(element)

and from the code you have given, I think num_of_emp should be an empty list.

num_of_emp = []

not an empty string----> ""

So, If you could be more specific, that would be helpful.

CodePudding user response:

def count_smth(records):
    result = 0
    for count in records:
        if condition:
            result  = 1
    return result

If you want to return number, specify result value as 0. Your function returns count, not the result.
You can pass in if any condition, even write another function that returns boolean value.
a = value syntax makes the same thing as a = a value

CodePudding user response:

Two options:

1 - In Python, in this case you can return a list containing only the best employees like the code below. Then you could simply get the count of these best employees accessing the length method of the list.

def count_employees(records):
   num_of_emp = []
   for employee in records: 
      if float(employee[3]) > 50000: 
         num_of_emp.append(employee) 
   return num_of_emp

However, you will need to create another list in the class where you call this function in order to get correctly the returned list. For instance in main.py

best_employees = []
records = [ <WHATEVER DATA YOU HAVE>]

best_employees = count_employees(records) #To get the list with the best employees
count_of_best_employees = best_employees.len() #To get the count of the best employees

2 - If you only want to get the counter then you would do this:

def count_employees(records):
   counter_best_employees = 0
   for employee in records: 
      if float(employee[3]) > 50000: 
         counter_best_employees = counter_best_employees   1
   
return counter_best_employees

Python is one of the highest level lenguages I know and probably there is some kind of function that returns directly what you want without iterating the list yourself, but this will do for now.

  • Related