Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Найти все элементы списка в ПИТОНЕ, равные нулю, и вычислить сумму квадратов индексов строк данных элементов.

Планово-финансовый отдел СЭАТ Ученик (184), открыт 2 дня назад
Не могу вычислить сумму
B=[[1,0,3,2],[0,1,0,0],[1,2,0,3],[0,0,2,3]]
for i in range(len(B)):
for j in range(len(B)):
if B[i][j]==0:
D=f'{pow((i),2)}'
for number in range(len(D)):
print(D)
1 ответ
Терр Онтал Мудрец (11586) 2 дня назад
Добро пожаловать в будущее:
 def sum_of_squared_row_indices(input_list): 
# Initialize the sum of squared row indices
sum_squared_row_indices = 0

# Iterate through the rows with their indices
for row_index, row in enumerate(input_list):
# Check if there are non-zero elements in the current row
if any(value != 0 for value in row):
# Add the square of the row index to the sum
sum_squared_row_indices += row_index ** 2

return sum_squared_row_indices

# Example usage
input_list = [
[0, 3, 0],
[4, 0, 5],
[0, 0, 0]
]
result = sum_of_squared_row_indices(input_list)
print("Sum of squared row indices of non-zero elements:", result)

Похожие вопросы