我正在尝试解决的一个有趣问题:
我需要限制要带队的员工人数(下面代码中的数字)
employee.txt 示例部分:
30;电气工程师; 1; 70
31;科学家; 0; 100
32; doctor; 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)我得到一个重复的数字
如何限制新列表添加到需要的数字后?
赞赏所有建议并指出错误
答案 0 :(得分:0)
尝试:
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()