List Conditional Summation Method
list_data=[ [1.0, 'Accessories', '522422', 'Buttons for Aluminum Button Board', 'Golden', '', 72.0, 'PC', ''], [2.0, 'Accessories', '500031', 'Cross recessed countersunk head self-drilling and tapping screws4.2*45', 'primary color', '', 72.0, 'PC', ''], [1.0, 'Accessories', '522422', 'Buttons for Aluminum Button Board', 'primary color', '', 24.0, 'PC', ''], [2.0, 'Accessories', '500031', 'steel nail', 'primary color', '', 24.0, 'PC', ''], [1.0, 'Accessories', '522422', 'Buttons for Aluminum Button Board', 'primary color', '', 50.0, 'PC', ''], [2.0, 'Accessories', '500031', 'Cross recessed countersunk head self-drilling and tapping screws4.2*45', 'primary color', '', 50.0, 'PC', ''] ] tiaojian=[1,2,3,4,5,7] QHX=6 def tj_sum(list_data,tiaojian,QHX):# (lists, summation condition columns, summation terms) zidian={} for i in range(0,len(list_data)): jian='' for p in range(0,len(tiaojian)): jian=jian+str(list_data[i][tiaojian[p]])+',' if jian in zidian: zidian[jian][QHX]=float(zidian[jian][QHX])+list_data[i][QHX] else: zidian[jian]=list_data[i] return list(()) jieguo=tj_sum(list_data,tiaojian,QHX)# Input parameters (lists, summation condition columns, summation terms) are numbered from 0. print(jieguo)
Using the list of summing functions (can take an infinite number of parameters to sum)
Note: the difference between append and direct assignment using subscripts
numbeer = []# Define empty lists with [] instead of {} def summ(numbeer): c = 0 for x in range(0, numbeer.__len__()):#range needs to be followed by a specific number a = numbeer[x] c += a x += 1 return c a = x = 0 while True: a = int(input('Please enter a parameter that requires a summation: ')) (a)The difference between #append and direct subscript assignment: append can still assign values even to an empty list, but subscript assignment does not apply to empty lists. x += 1 c = input('To continue press the space bar or exit') if c != ' ': break c = summ(numbeer)# Use a function with a return value that must be given a variable to store the return value print(c)
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.