Используя метод numpy.fill_diagonal()
, вы можете заполнить диагонали массива numpy значением, переданным в качестве параметра в numpy.fill_diagonal ( ) .
Синтаксис: numpy.fill_diagonal (массив, значение)
Возврат: Возвращает заполненный значение по диагонали массива.
Пример № 1:
В этом примере мы видим, что с помощью numpy.fill_diagonal()
мы можем получить диагонали заполнены значениями, переданными в качестве параметра.
# NumPy import import numpy as np # с использованием метода numpy.fill_diagonal() array = np.array([[ 1 , 2 ], [ 2 , 1 ]]) np .fill_diagonal (массив, 5 ) print (массив) |
Вывод:
[[5 2]
[2 5]]
Пример #2:
< /p>
# NumPy import import numpy as np < br /> # с помощью метода numpy.fill_diagonal() array = np.zeros (( 3 , 3 ), int ) np.fill_diagonal (массив, 1 ) print (массив) |
Вывод:
[ [1 0 0]
[0 1 0]
[0 0 1]]
Мы надеемся, что эта статья помогла вам решить проблему. Помимо Python | метод numpy.fill_diagonal(), проверьте другие темы, связанные с Python functions.
Хотите преуспеть в Python? Посмотрите наш обзор лучших онлайн-курсов Python 2023. Если вас интересует наука о данных, узнайте также, как изучить программирование на R.
Кстати, этот материал доступен и на других языках:
Dmitry Galleotti
Vigrinia | 2023-03-25
Python functions всегда немного напрягает 😭 Python | метод numpy.fill_diagonal() это не единственная проблема, с которой я столкнулся. Вчера проверил, работает!
Cornwall Wu
Vigrinia | 2023-03-25
Python functions всегда немного напрягает 😭 Python | метод numpy.fill_diagonal() это не единственная проблема, с которой я столкнулся. Я просто надеюсь, что эта проюлема больше не возникнет
Javier Galleotti
Berlin | 2023-03-25
Я готовился к собеседованию по программированию., спасибо за разъяснение - Python | метод numpy.fill_diagonal() в Python не самый простой.. Я просто не совсем уверен, что это лучший метод
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically