Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python get the subscript of an element in array

Shulou Source: shulou.com Published: 2022-06-01 13:38:59 09月21日 Update

Editor to share with you how python gets the subscript of the element in array, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's go to know it together.

In numpy, we can use array as a container for an array (or list).

Create an array list with numpy > import numpy > vector = numpy.array ([5, 10, 15 and 15]) > print (vector)

[5 10 15 20]

Use the where method of numpy to find a value under the array > a = numpy.where (vector==10) > print (a)

(array ([1], dtype=int64),)

The returned data is the corresponding subscript

The subscript is wrapped in a tuple and a list. We can easily take it out with a [0] [0]:

> print (a [0] [0])

one

Add: how does python get the subscript position of each element in the list

The first method for i in range (len (numbers)): print ('({0}, {1}) '.format (I, numbers [I])) the second method

Use the enumerate function

Numbers = [10,29,30,41] for index, value in enumerate (numbers): print (index, value) the third kind

It is also the most effective index built-in function.

List1 = [1 print 22 31 1print (f 2) # 8 "is all the contents of the article" how to get the subscript of elements in array ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Subscript element method article content function array valid not so location most container data more knowledge industry package information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi NVidia OPPO Reno Apple