简体   繁体   中英

Single list.count instead of multiple

Im parsed list of crew witch one looks like:

20;mechanic;0;68

21;cook;0;43

22;scientist;0;79

23;manager;1;65

24;mechanic;1;41

etc

And now I'm trying to figure out how to count number of workers who have 60 or more stamina( the last element in each employee )

There is my code:

with open('employee.txt', 'r') as employee_list:
    count = 0
    for employee in employee_list.readlines():
        employee_data = employee.rstrip().split(';')
        if int(employee_data[3]) >= 60:
            count += 1
        print(count)

Print from terminal:

1

2

3

...

90

And there is the right answer I think, but is there anyway to get only one 'total' count, not a 90ty strings ?

Just print one line after the loop is done.

with open('employee.txt', 'r') as employee_list:
    count = 0
    for employee in employee_list.readlines():
        employee_data = employee.rstrip().split(';')
        if int(employee_data[3]) >= 60:
            count += 1
    print(count)

But I would also recommend using pandas for data manipulation. For example:

df = pd.read_csv('employee.txt', sep=';')
df.columns = ['col1', 'col2', 'col3', 'stamina']

Then just filter and get the size:

df[df.stamina >= 60].size

So after a day of thinking I wrote this and get right answer ( maybe someone will find this helpful):

def total_resist_count():

    # with open('employee.txt', 'r') as employee_list:
    employee_list = [input() for i in range(120)]

    candidates = []

    for employee in employee_list:
        employee_data = employee.rstrip().split(';')
        if int(employee_data[3]) >= 60:
            candidates.append(employee_data)
    return candidates


required_professionals = {
    'computers specialist': 5,
    'cook': 3,
    'doctor': 5,
    'electrical engineer': 4,
    'manager': 1,
    'mechanic': 8,
    'scientist': 14
}

expedition_total = 40
female_min = 21
male_min = 12


def validate_solution(cur_team, num_females, num_males):
    global expedition_total, female_min, male_min
    if sum(cur_team) != expedition_total or num_females < female_min or num_males < male_min:
        return False
    num_of_free_vacancies = 0
    for k in required_professionals:
        num_of_free_vacancies += required_professionals[k]
    if num_of_free_vacancies > 0:
        return False
    return True


TEAM = None


def backtrack(candidates, cur_team, num_females, num_males):
    global required_professionals, expedition_total, TEAM
    if sum(cur_team) > expedition_total or TEAM is not None:
        return
    if validate_solution(cur_team, num_females, num_males):
        team = []
        for i, used in enumerate(cur_team):
            if used == 1:
                team.append(candidates[i])
        TEAM = team
        return

    for i in range(len(candidates)):
        if cur_team[i] == 0 and required_professionals[candidates[i][1]] > 0:
            cur_team[i] = 1
            required_professionals[candidates[i][1]] -= 1
            if candidates[i][2] == '1':
                backtrack(candidates, cur_team, num_females, num_males + 1)
            else:
                backtrack(candidates, cur_team, num_females + 1, num_males)
            required_professionals[candidates[i][1]] += 1
            cur_team[i] = 0


if __name__ == '__main__':
    ec = decode_fcc_message()
    candidates = total_resist_count(ec)
    cur_team = [0] * len(candidates)
    backtrack(candidates, cur_team, 0, 0)
    s = ""
    for t in TEAM:
        s += str(t[0]) + ';'
    print(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM