Python TypeError:在嵌套列表中执行某些计算时,列表索引必须是整数或切片,而不是元组
我试图在嵌套列表中进行一些计算 示例是Python TypeError:在嵌套列表中执行某些计算时,列表索引必须是整数或切片,而不是元组,python,Python,我试图在嵌套列表中进行一些计算 示例是[['Amy',2,3,4],'Jack',3,4,None],我希望看到如下输出: [[3.0,'Amy',[3.5,'Jack']](3.0表示2,3,4的平均值,3.5表示3,4的平均值) 我的代码: def compute_mean_pc(): students_pclist=[['Amy',2,3,4],['Jack',3,4,None]] mean_pc=[[[countMean(students_pclist[element][
[['Amy',2,3,4],'Jack',3,4,None]
,我希望看到如下输出:
[[3.0,'Amy',[3.5,'Jack']]
(3.0表示2,3,4的平均值,3.5表示3,4的平均值)
我的代码:
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()
第二部分,countMean(数组)运行良好,但对于第一部分,在这一行中
mean_pc=[[[countMean(students_pclist[element][1:])]for element in enumerate(students_pclist)]+[element[0]]for element in students_pclist]
Python返回一个类型错误:列表索引必须是整数或片,而不是元组
我的代码有什么问题?
对于枚举(学生列表)
中的元素,将向元素分配一个元组(索引,学生列表中的元素)
你想要的是:
[[countMean(element[1:]), element[0]] for element in students_pclist]
对于枚举(学生列表)中的元素
将向元素
分配一个元组(索引,学生列表中的元素)
你想要的是:
[[countMean(element[1:]), element[0]] for element in students_pclist]
代码中错误的部分是列表理解中enumerate(students_pclist)
元素的:在每个迭代循环中返回一个元组。所以您应该在enumerate(students\u pclist)
中为元素i编写类似的代码。
它修复了您的错误,但没有给您预期的答案
以下是基于您的代码的完整修复建议:
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']]
最后,我建议您使用一个好的老式for循环,编写一个更高效、可读性更好的代码:
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]]
代码中错误的部分是列表理解中enumerate(students_pclist)元素的
:在每个迭代循环中返回一个元组。所以您应该在enumerate(students\u pclist)
中为元素i编写类似的代码。
它修复了您的错误,但没有给您预期的答案
以下是基于您的代码的完整修复建议:
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']]
最后,我建议您使用一个好的老式for循环,编写一个更高效、可读性更好的代码:
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]]
您在正确使用
枚举返回的索引时遇到问题。我只是用正确的方式使用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)
输出
[[3.0, 'Amy'], [3.5, 'Jack']]
您在正确使用枚举返回的索引时遇到问题。我只是用正确的方式使用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)
输出
[[3.0, 'Amy'], [3.5, 'Jack']]
这一个应该满足您的需要:
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']]
这一个应该满足您的需要:
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']]
您可以使用以下函数计算平均值:
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()
您可以使用以下函数计算平均值:
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()
谢谢你的帮助^ ^谢谢你的帮助^_^