Skip to content Skip to sidebar Skip to footer

Typeerror: List Indices Must Be Integers Or Slices, Not Tuple While Doing Some Calculation In A Nested List

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,

Solution 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=0for 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, *valuesin myList:            # iterate overeach sub-list getting name and next valuesvalues= 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
    returnresultresult= compute_mean_pc(myList)

print(result) # [['Amy', 3.0], ['Jack', 3.5]]

Solution 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]

Solution 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']]

Solution 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] forsubin a]
result
#[[3.0, 'Amy'], [3.5, 'Jack']]

Solution 5:

You can use below function to count mean:

defcompute_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)


defcount_mean(array):
    grades = [el for el in array ifisinstance(el, int)]
    returnsum(grades) / len(grades)

compute_mean_pc()

Post a Comment for "Typeerror: List Indices Must Be Integers Or Slices, Not Tuple While Doing Some Calculation In A Nested List"