如何限制lis中追加元素的计数

2024-09-22 16:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解决一个有趣的问题:

我需要限制我带到expedition的员工数量(下面代码中的数字)

employee.txt示例部分:

30;electrical engineer;1;70

31;scientist;0;100

32;doctor;0;42

33;scientist;1;47

34;mechanic;0;63

我按性别和职业列出了他们的总数。 这是我的密码:

manager = 1
cook = 3
electrical_engineer = 4
computers_specialist = 5
doctor = 5
mechanic = 8
scientist = 14

expedition_total = 40
female_min = 21
male_min = 12

def total_resist_count():

    with open('employee.txt', 'r') as employee_list:

        total_count = 0
        female_count = 0
        male_count = 0
        expedition = []

        for employee in employee_list.readlines():
            employee_data = employee.rstrip().split(';')
            if int(employee_data[3]) >= 60:
                total_count += 1

            if int(employee_data[2] == '0') and int(employee_data[3]) >= 60:
                female_count += 1

            if int(employee_data[2] == '1') and int(employee_data[3]) >= 60:
                male_count += 1

        print('Stress-resistant colonists: ', total_count)
        print('--------- Female colonists: ', female_count)
        print('----------- Male colonists: ', male_count)
        print('--------------------------')
        print('Mars expedition list: ')
        pprint(expedition)


if __name__ == '__main__':
    total_resist_count()

OUTPUT:

Stress-resistant colonists: 90

--------- Female colonists: 48

----------- Male colonists: 42


Mars expedition list:

[]

我需要把每一位专家加入到探险队中(总共限制在代码-40中的var)

我试着:

 if int(employee_data[2] == '0') and int(employee_data[3]) >= 60:
        expedition.append(employee_data)
        female_count += 1

以及:

if int(employee_data[2] == '0') and int(employee_data[3]) >= 60:
            female_count += 1
            for female_employee in range(total_count - male_min):
                expedition.append(female_employee)

1)但是我得到的是拥有员工数据的员工的完整列表[2]==“0”
员工数据[3])>;=60(显然)

2)我得到一个重复的数字

How I can limit new list append to the numbers I needed ?

Appreciate any advices and pointing to mistakes


Tags: anddataifcount员工employeemalefemale
1条回答
网友
1楼 · 发布于 2024-09-22 16:41:46

尝试:

manager = 1
cook = 3
electrical_engineer = 4
computers_specialist = 5
doctor = 5
mechanic = 8
scientist = 14

expedition_total = 40
female_min = 21
male_min = 12

    def total_resist_count():

        with open('employee.txt', 'r') as employee_list:

            total_count = 0
            female_count = 0
            male_count = 0
            expedition = []

            for employee in employee_list.readlines():
                employee_data = employee.rstrip().split(';')
                if int(employee_data[2] == 0) and int(employee_data[3]) >= 60:
                    female_count += 1
                    total_count += 1
                    expedition.append(employee_data)

                elif int(employee_data[2] == 1) and int(employee_data[3]) >= 60:
                    male_count += 1
                    total_count += 1
                    expedition.append(employee_data)

        print('Stress-resistant colonists: ', total_count)
        print('    - Female colonists: ', female_count)
        print('     - Male colonists: ', male_count)
        print('             ')
        print('Mars expedition list: ')
        print(expedition)


    if __name__ == '__main__':
        total_resist_count()

如果需要更具体的参数,请将它们添加到条件语句中。例如,如果您需要某一类型员工的最小/最大数量,只需检查该类型员工的运行总数是否在范围内,如果在范围内,则相应地追加或忽略

如果您最多可以容纳40人,请使用:

manager = 1
cook = 3
electrical_engineer = 4
computers_specialist = 5
doctor = 5
mechanic = 8
scientist = 14

expedition_total = 40
female_min = 21
male_min = 12

total_count = 0
female_count = 0
male_count = 0
expedition = []

    def total_resist_count():
        global total_count
        global female_count
        global male_count
        global expedition

        with open('employee.txt', 'r') as employee_list:
            while len(expedition) < total_count:

                for employee in employee_list.readlines():
                    employee_data = employee.rstrip().split(';')
                    if int(employee_data[2] == '0') and int(employee_data[3]) >= 60:
                        female_count += 1
                        total_count += 1
                        expedition.append(employee_data)

                    elif int(employee_data[2] == '1') and int(employee_data[3]) >= 60:
                        male_count += 1
                        total_count += 1
                        expedition.append(employee_data)

        print('Stress-resistant colonists: ', total_count)
        print('    - Female colonists: ', female_count)
        print('     - Male colonists: ', male_count)
        print('             ')
        print('Mars expedition list: ')
        print(expedition)


    if __name__ == '__main__':
        total_resist_count()

相关问题 更多 >