Python dict ndarray
It seems like there might be a little confusion in your question. In Python, `dict` is a built-in data type that stores a collection of key-value pairs. On the other hand, `ndarray` (often just referred to as an array) is a type provided by the `NumPy` library that represents a multi-dimensional, homogeneous array of fixed-size items.
I assume you're looking for examples that showcase both. Here's a brief introduction:
### 1. Python `dict`:
```python
person = {
"name": "John",
"age": 30,
"city": "New York"
}
print(person["name"]) # Outputs: John
```
### 2. `numpy ndarray`:
First, you need to install numpy:
```bash
pip install numpy
```
Then you can create arrays:
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr) # Outputs: [1 2 3 4 5]
print(type(arr)) # Outputs: <class 'numpy.ndarray'>
```
### Combining both:
Here's an example of using a dictionary where the values are `ndarray` objects:
```python
import numpy as np
data = {
"temperatures": np.array([22.5, 23.0, 23.5, 24.0, 22.0]),
"humidity": np.array([0.55, 0.52, 0.53, 0.50, 0.51])
}
print(data["temperatures"]) # Outputs: [22.5 23. 23.5 24. 22. ]
print(data["humidity"]) # Outputs: [0.55 0.52 0.53 0.5 0.51]
```
In the above example, the dictionary `data` contains two keys, `"temperatures"` and `"humidity"`, and their corresponding values are `ndarray` objects containing related measurements.
Comments
Post a Comment