подсчитать конкретное число в 3-м списке - PullRequest
0 голосов
/ 07 мая 2019

Я создал 3d список. теперь я хочу посчитать вхождения чисел в этот список 3d.

print("Enter matrix entries column wise one matrix after another:\n")
lists=[[[int(input()) for _ in range(o)] for _ in range(a)] for _ in  range(c)] 
print(lists)

Я сделал этот код для создания 3d списка. Здесь o = 4 (количество строк), a = 3 (количество столбцов), c = 2 (номер матрицы) (ранее задано пользователем) и матрица содержит только 1,2,3,4 и 5.

Enter matrix entries column wise one matrix after another: 
 1 
 1 
 2 
 3 
 4 
 5
 1 
 2 
 3 
 4 
 5 
 1 
 2 
 1 
 1 
 1 
 1 
 1 
 1 
 2 
 2 
 3 
 3 
 4 
 4 
 [[[1, 2, 3, 4], [5, 1, 2, 3], [4, 5, 1, 2]], [[1, 1, 1, 1], [1, 1, 2, 2], [3, 3, 4, 4]]]

и я получил этот вывод, и это желательный случай. Приведенный 3d список можно считать

matrix1= 1 5 4
         2 1 5
         3 2 1
         4 3 2
matrix2= 1 2 3
         1 2 3
         1 2 3
         1 2 3

Теперь я хочу, чтобы в 3d-списке было указано «количество», которое выглядит следующим образом:

[[[1, 1, 1], [1, 1, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1]], [[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [0, 0, 0]]]

Here counts[0][0][0](i.e. first 1) describes how many 1's are there in first column first matrix.

counts[0][0][1](i.e. second 1) describes how many 1's are there in second column first matrix.

counts[0][0][2](i.e. third 1) describes how many 1's are there in third column first matrix.

counts[0][1][0](i.e. forth 1) describes how many 2's are there in first column first matrix.

counts[0][1][1](i.e. fifth 1) describes how many 2's are there in second column first matrix.

counts[0][1][2](i.e. sixth 1) describes how many 2's are there in third column first matrix.

counts[0][2][0](i.e. seventh 1) describes how many 3's are there in first column first matrix.

counts[0][2][1](i.e. eighth 1) describes how many 3's are there in second column first matrix.

counts[0][2][2](i.e. ninth 0) describes how many 3's are there in third column first matrix.

counts[0][3][0](i.e. tenth 1) describes how many 4's are there in first column first matrix.

counts[0][3][1](i.e. eleventh 0) describes how many 4's are there in second column first matrix.

counts[0][3][2](i.e. 12th 1) describes how many 4's are there in third column first matrix.

counts[0][4][0](i.e. 13th 0) describes how many 5's are there in first column first matrix.

counts[0][4][1](i.e. 14th 1) describes how many 5's are there in second column first matrix.

counts[0][4][2](i.e. 15th 1) describes how many 5's are there in third column first matrix.

Таким же образом [[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [0, 0, 0]] эта часть также рассчитывается для второй матрицы. Пожалуйста, вычислите список «count» из списка «lists» и переменных «a», «o», «c».

1 Ответ

0 голосов
/ 07 мая 2019

Я не уверен, какой именно вывод вы ищете, но вы можете использовать циклы для этого:

lists=[[[1, 2, 3, 4], [5, 1, 2, 3], [4, 5, 1, 2]], [[1, 1, 1, 1], [1, 1, 2, 2], [3, 3, 4, 4]]]
for m_num, matrix in enumerate(lists):
    for ind, col in enumerate(matrix):
        for i in range(1, 6):  # because as you said matrix contains only 1, 2, 3, 4 and 5
            print('number of {} in column idn: {}, of {} matrix: {}'.format(i, ind+1, m_num+1, col.count(i)))

Вы также можете пропустить перечисление и просто напечатать числа '1' в первом столбце, затем во втором столбце и т. Д .:

lists=[[[1, 2, 3, 4], [5, 1, 2, 3], [4, 5, 1, 2]], [[1, 1, 1, 1], [1, 1, 2, 2], [3, 3, 4, 4]]]
for matrix in lists:
    for col in matrix:
        for i in range(1, 6):
            print(col.count(i))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...