-
-
Notifications
You must be signed in to change notification settings - Fork 307
Article: Data-Structure-ARRAYS.md #1052
Changes from 6 commits
589f306
1d915ca
317b0b1
653e0c8
9d99fc8
950b0db
9cac6af
4b7db7f
1267cb2
8560fa2
8a46732
7caf09c
12d5c6c
65f85a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#Arrays | ||
|
||
`array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store | ||
a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type. | ||
|
||
`array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preface with Internally, or under the hood. But remember, languages like Python might not have this as true. Heck, any language that runs on a VM (Java, Ruby, Python, JS etc.), you probably cannot guarantee that. But these languages still implement Arrays. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding which part of the definition do you want me to do the changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just start with something, that clearly says this is not a necessary requirement to be an Array. As in, in C, this would be true. But at a high-level scripting language, it might not be true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a new line here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atjonathan Wanted to explain a different point about the memory allocation of arrays so a new paragraph |
||
|
||
## Arrays in Python | ||
|
||
Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` | ||
and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. | ||
|
||
####Declaration of `array`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atjonathan Done |
||
|
||
```python | ||
from array import array | ||
intarray = array('i') # Declares an array of integer type | ||
``` | ||
|
||
####Adding elements to `array`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
```python | ||
intarray.append(1) # Adds an integer value of 1 to the array | ||
intarray.append(0) # Adds an integer value of 0 to the array | ||
intarray.append(-1) # Adds an integer value of -1 to the array | ||
intarray.append(1) # Again adds an integer value of 1 to the array | ||
|
||
intarray.append('d') # Would give a TypeError as the array is of integer type. | ||
|
||
#Resolve the above error and then move ahead. | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add REPL snippet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. Please check |
||
####Printing an `array`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
```python | ||
print(intarray) # Returns array('i', [1, 4, -1]) | ||
print(intarray[0]) # Returns 1 which is the element at index 0 of the array | ||
print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. | ||
|
||
#Resolve the above error and then move ahead. | ||
|
||
# To print all the elements of the array | ||
for i in intarray: | ||
print(i) | ||
``` | ||
|
||
####Basic operations on `array`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
```python | ||
len(intarray) # Returns the length of the array i.e. 3 | ||
intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer | ||
intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 | ||
intarray.insert(1, 3) # Insert a new item with value x in the array before position i | ||
intarray.remove(1) # Remove the first occurrence of 1 from the array | ||
intarray.reverse() # Reverse the order of the items in the array | ||
intarray.pop(1) # Removes the item with the index 1 from the array and returns it | ||
``` | ||
|
||
:rocket: [Run Code](https://repl.it/CWJB) | ||
|
||
[Official Docs](https://docs.python.org/3.5/library/array.html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use
:
on the first title, they break the generator and get removed anyways.