【问题标题】:How to limit append elements count in list如何限制列表中的附加元素计数
【发布时间】:2018-08-15 21:58:01
【问题描述】:

我正在尝试解决的一个有趣的问题:

我需要限制我带去探险的员工人数(下面我的代码中的数字)

employee.txt 示例部分:

30;电气工程师;1;70

31;科学家;0;100

32;医生;0;42

33;科学家;1;47

34;机械师;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()

输出:

抗压殖民者:90

--------- 女性殖民者:48

----------- 男性殖民者:42


火星探险名单:

[]

我需要将每个专家计数添加到远征中(仅限于代码中的 var - 总共 40 个)

我试试:

 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) 但我得到了具有 employee_data[2] == '0'
employee_data[3]) >= 60 的员工的完整列表>(显然)

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

如何限制新列表追加到我需要的数字?

感谢任何建议并指出错误

【问题讨论】:

  • 能解释一下employee.txt中数字的含义吗?在火星探险名单中你想要什么样的数据?只是一个数字?还是完整的数据?如果您需要的话,您可以轻松制作一个数据列表列表,每个子列表代表一个员工。
  • 如果您打印的列表女巫包含这些元素,它将如下所示:[1,doctor,0,76] - id,职业,性别,级别,如果您为此调用 employee_data[2],您将得到'0'

标签: python append limit


【解决方案1】:

尝试:

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()

【讨论】:

  • 这只是添加了我所有的 120 名员工进行探险
  • 已编辑以解决此问题。如果您也需要一定数量的特定类型员工,我可以提供帮助,而不仅仅是列出的前 40 名员工
  • 更具体的其他参数,我可以继续提供帮助
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多