可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to do some calculation in nested lists
The example is [['Amy',2,3,4],['Jack',3,4,None]]
, and I want to see the output like: [[3.0,'Amy'],[3.5,'Jack']]
(3.0 is mean of 2,3,4 and 3.5 is mean of 3,4)
My code:
def compute_mean_pc(): students_pclist=[['Amy',2,3,4],['Jack',3,4,None]] mean_pc=[[[countMean(students_pclist[element][1:])]for element in enumerate(students_pclist)]+[element[0]]for element in students_pclist] print(mean_pc) def countMean(array): count=0 sumup=0 for i in range(len(array)): if array[i]!=None: count+=1 sumup+=array[i] mean=sumup/count return mean compute_mean_pc()
the second part, countMean(array) works well, but for the first part,in this line
mean_pc=[[[countMean(students_pclist[element][1:])]for element in enumerate(students_pclist)]+[element[0]]for element in students_pclist]
Python returns a type error: list indices must be integers or slices, not tuple
What's wrong with my code?
回答1:
The wrong part in your code is for element in enumerate(students_pclist)
inside your list comprehension: enumerate() returns a tuple on each iteration loop. So you should have written something like for element,i in enumerate(students_pclist)
. It fixes your error, but it does not give you the expected answer.
Here is a suggestion of complete fix, based on your code:
myListOfLists = [['Amy',2,3,4], ['Jack',3,4,None]] def compute_mean_pc(): students_pclist=[['Amy',2,3,4],['Jack',3,4,None]] mean_pc=[ [countMean(student[1:])] +[student[0]] for student in students_pclist] print(mean_pc) def countMean(array): count=0 sumup=0 for i in range(len(array)): if array[i]!=None: count+=1 sumup+=array[i] mean=sumup/count return mean compute_mean_pc() # [[3.0, 'Amy'], [3.5, 'Jack']]
And finally I suggest you a code which is more efficient and still readable, using a good old-fashioned for loop:
myList = [['Amy',2,3,4], ['Jack',3,4,None]] def compute_mean_pc(myList): result = [] for name, *values in myList: # iterate over each sub-list getting name and next values values = list(filter(None,values)) # Remove any 'None' from the values result.append([name, sum(values)/len(values)]) # Append a list [name,mean(values)] to the result list return result result = compute_mean_pc(myList) print(result) # [['Amy', 3.0], ['Jack', 3.5]]
回答2:
for element in enumerate(students_pclist)
will assign a tuple (index, element_of_students_pclist)
to element
.
What you want is:
[[countMean(element[1:]), element[0]] for element in students_pclist]
回答3:
You had problems with correctly using index returned by the enumerate
. I just slightly modified your code with the correct way of using enumerate
def compute_mean_pc(): students_pclist=[['Amy',2,3,4],['Jack',3,4,None]] mean_pc=[[ countMean(students_pclist[i][1:]) ] + [element[0]] for i, element in enumerate(students_pclist)] print(mean_pc)
Output
[[3.0, 'Amy'], [3.5, 'Jack']]
回答4:
This one should do what you need:
a = [['Amy',2,3,4],['Jack',3,4,None]] def computeMean(array): valid = [i for i in array[1:] if i] return [sum(valid)/len(valid)] result = [computeMean(sub) + sub[:1] for sub in a] result #[[3.0, 'Amy'], [3.5, 'Jack']]
回答5:
You can use below function to count mean:
def compute_mean_pc(): students_pclist=[['Amy',2,3,4],['Jack',3,4,None]] mean_pc=[ [student[0], count_mean(student)] for student in students_pclist] print(mean_pc) def count_mean(array): grades = [el for el in array if isinstance(el, int)] return sum(grades) / len(grades) compute_mean_pc()