有更好或更短的编写此代码的方式吗?
def business_role_code_to_name(x):
y = []
for position, code in enumerate(x):
if position == 0 and code == 1.0:
y.append("Owner")
elif position == 1 and code == 1.0:
y.append("Manager")
elif position == 2 and code == 1.0:
y.append("Employee")
elif position == 3 and code == 1.0:
y.append("Other")
return y
assert business_role_code_to_name([1.0, 1.0, 1.0, 0.0]) == ['Owner', 'Manager', 'Employee']
assert business_role_code_to_name([0.0, 1.0, 0.0, 1.0]) == ['Manager', 'Other']
我是编程新手,我认为有更好的方法来编写这段代码。谢谢!
if
中您都检查code==1.0
,因此您可以在for
循环开始时仅检查一次,这样更有效率。 - Omri Attiya1.0
)还是整数(1
)? - Andrew Jaffe