Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get all the elements in the Python array and the corresponding subscript index values

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to get all the elements in the Python array and the corresponding subscript index values". In the daily operation, I believe that many people have doubts about how to get all the elements in the Python array and the corresponding subscript index values. The editor consulted all kinds of data and sorted out a simple and useful operation method. I hope it will be helpful for you to answer the question of "how to get all the elements in the Python array and the corresponding subscript index values"! Next, please follow the editor to study!

1. For cycle

The easiest way to get the element and location subscript is to iterate through the for loop. The code is as follows:

Import random

# get random array

Array = []

For i in range (10):

X = random.randint (1,100)

Array.append (x)

# iterate through the array, printing subscript values and elements

For j in range (len (array)):

Print ("{} {}" .format (j, array [j]) 2, enumerate () function

The second method is to use the enumerate () function. This function can be used to combine a traversable data object (such as an array, tuple, string, etc.) into an index sequence, while listing data and data subscript. Generally, it is used with the for loop. The code is as follows:

Import random

Array = []

For i in range (10):

X = random.randint (1,100)

Array.append (x)

For x, y in enumerate (array):

Print ("{} {}" .format (x, y))

This function also allows us to customize which subscript value to start with. For example, I assume that the subscript value of the first element is 3:

Import random

Array = []

For i in range (10):

X = random.randint (1,100)

Array.append (x)

For x, y in enumerate (array, start=3):

Print ("{} {}" .format (x, y)) 3. Import the Pandas library

Pandas library is an important library for Python to carry out data science, and it is of great help to data research. The two data types in Pandas, Series and DaTaFrame, automatically help us fill in the superscript values, and also allow us to customize the subscript index values. This custom subscript index can be either numeric or character. And, when the result is printed, it will tell us what data type the array is. As follows:

Import random

Import pandas as pd

Array = []

For i in range (10):

X = random.randint (1,100)

Array.append (x)

Sr = pd.Series (array, ['axiomagento, pachydermis,)

Print (sr) this is the end of the study on "how to get all the elements in the Python array and the corresponding subscript index values". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report