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 does Python cycle through Array in Numpy

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how Python cycle through the Array in Numpy, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. Introduction

Numpy is a common data processing library in Python. Numpy, which stands for Numerical Python, is a frequently used library in data science. Numpy is designed to handle matrix operations because it contains a wide variety of handlers. In this article, we are mainly used to learn how to iterate through the elements in the access matrix.

Cut the gossip and let's get started.

two。 Traversing using For loops

First, let's look at an example that uses a loop to iterate through the array. The sample code is as follows:

Import numpy as nparray = np.array ([1,2,3,4,5,6]) for x in array: print (x) Output:123456

In the above example, we created an one-dimensional array and successfully iterated through each value. Now let's look at an example in a two-dimensional matrix:

Import numpy as nparray = np.array ([[1,2,3], [4,5,6]]) for x in array: for y in x: print (y) Output:123456

As we can see in the above example, we can still print out each individual value. Because it is a two-dimensional array, we must use two for loops to output each individual value. This is the way we usually iterate over two-dimensional arrays, but NumPy provides us with new functions that make it easier to iterate over NumPy arrays.

3. Function nditer ()

The function nditer () is mainly used to iterate through the array without having to use nested for loops for each additional dimension.

Let's take a look at an example:

Import numpy as nparray = np.array ([[1,2,3], [4,5,6], [7,8,9]]) for x in np.nditer (array): print (x) Output:123456789

The above example is a two-dimensional array, and after we use the function nditer (), we no longer need to use a nested for loop. The function 'nditer ()' successfully accesses and prints each value in the array.

Let's take a look at an example of a three-dimensional array, as follows:

Import numpy as nparray = np.array ([1], [2], [[3], [4]) for x in np.nditer (array): print (x) Output:1234

As we can see in the above example, the function nditer () successfully iterates over each element in the three-dimensional array.

4. Function ndenumerate ()

Then we introduce the function ndenumerate (), which outputs the corresponding value of the corresponding index number.

The sample code is as follows:

Import numpy as nparray = np.array ([1,2,3,4,5,6]) for I, x in np.ndenumerate (array): print (I, x) Output: (0,) 1 (1,) 2 (2,) 3 (3,) 4 (4,) 5 (5,) 6

As in the example above, we output the index number of each element and its corresponding value in parentheses. Then let's look at an example of a two-dimensional matrix:

Import numpy as nparray = np.array ([[1,2,3], [4,5,6]]) for I, x in np.ndenumerate (array): print (I, x) Output: (0,0) 1 (0,1) 2 (0,2) 3 (1,0) 4 (1,1) 5 (1,2) 6

In the above example, the first dimension we output represents the index number of each element, and the second dimension represents the value of each element.

This is all the content of the article "how to cycle through Array in Numpy by Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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