问题:
**已知有以下列表:
员工编号,姓名,基本工资,奖金,住房补助,五险一金,其他扣款,专项附加扣除
incomeList = [
['10932','张珊',15000.00 ,4000.00 ,2300.00 ,980.00 ,300.00 ,1000.00],
['10933','李思',12000.00 ,5000.00 ,2600.00 ,890.00 ,400.00 ,2000.00],
['10934','王武',10100.00 ,2000.00 ,2500.00 ,860.00 ,500.00 ,1500.00],
['10935','赵柳',10050.00 ,1000.00 ,2700.00 ,1080.00 ,300.00 ,2000.00],
['10936','钱棋',10200.00 ,2000.00 ,2400.00 ,1000.00 ,600.00 ,1000.00],
['10941','张明',13600.00 ,4000.00 ,2100.00 ,680.00 ,300.00 ,1000.00],
['10942','赵敏',13200.00 ,2500.00 ,2300.00 ,670.00 ,400.00 ,3000.00],
['10945','王红',13600.00 ,2600.00 ,2030.00 ,560.00 ,400.00 ,2000.00],
['10946','李萧',12500.00 ,2800.00 ,2400.00 ,420.00 ,500.00 ,1500.00],
['10947','孙科',12000.00 ,3500.00 ,2030.00 ,880.00 ,300.00 ,1000.00],
['10948','刘利',14200.00 ,2500.00 ,2020.00 ,900.00 ,400.00 ,2000.00]
]
编写程序,根据用户输入的表达式进行列表降序排序后,输出员工编号,编号之间用逗号分隔。例如,用户输入2+3,代表需要按照第2项(基本工资)与第3项(奖金)的和进行降序排列。注:表达式中仅允许出现:2、3、4、5、6、7、+、-这几个字符。完成以下三个输入样例的测试,粘贴结果。
代码:
factor=input()
def printincomelist():
print (','.join(x[0]for x in incomeList))
if factor.find('-') ==0:
list1=factor.split('+')
def f(x):
k=0
for i in list1:
k+=x[int(i)]
return k
incomeList.sort(key=f)
printincomelist()
else:
if factor.find('+')==0:
list2=fator.split('-')
def k(x):
p=x[int(list2[0])]
for i in list2 and i >0:
p-=x[int(i)]
return p
incomeList.sort(key=k)
printincomelist()
elif factor.find('+')!=0:
list3 = factor.split('-')
def l(x):
if list3[0].find('+')==0:
p=x[int(list3[0])]
else:
list4=list3[0].split('+')
p=0
for i in list4:
p+=x[int(i)]
for i in list3[1:]:
if i.find('+')==0:
p-=x[int(i)]
else:
list5=i.split('+')
p-=x[int(list5[0])]
for w in list5[1:]:
p+=x[int(list5[int(w)])]
return p
incomeList.sort(key=l)
printincomelist()