Manipulating Single-Dimension Arrays

After you get the data into an array, you’ll probably want to manipulate the data in specific ways. For example, you’ve already discovered that you can change a value in Listing 4-2. However, simply changing values won’t be enough. Listing 4-7 shows a number of ways in which you can work with array data (and IronPython provides considerably more methods than those shown in the listing).

Listin g 4-7: Manipulating array elements

[code]

# Define a function for printing.
def Show(type, array):
print ‘n’, type
for String in array:
print String
# Create the array.
MyList = [‘Zero’, ‘One’, ‘Two’]
# Display the number of elements.
print ‘Elements in MyList:’,
print len(MyList)
# Add a new element.
MyList.append(‘Two’)
Show(‘Appended a Value’, MyList)
# Add multiple elements.
MyList.extend([‘Three’, ‘Four’])
Show(‘Extended MyList’, MyList)
# Display the number of instances of the word ‘Two’.
print ‘nNumber of instances of Two:’,
print MyList.count(‘Two’)
# Remove one of the instances of ‘Two’ from the array.
MyList.remove(‘Two’)
Show(‘Removed a Value’, MyList)
# Pop the last value in the array.
MyList.pop()
Show(‘Popped a Value’, MyList)
# Delete a value.
del MyList[2]
Show(‘Deleted a Value’, MyList)
# Sort the array.
MyList.sort()
Show(‘Sorted the List’, MyList)
# Reverse the sort order.
MyList.reverse()
Show(‘Reversed the List’, MyList)
# Pause after the debug session.
raw_input(‘Press any key to continue…’)

[/code]

The code begins by defining Show(), a function used to display the array elements and the kind of manipulation performed on the array. This is a simple method to display information from the example without repeating the code. You’ve already seen this code in a number of examples, so you already know how it works.

Array manipulation falls into two categories. The first is using an external function to perform the task. For example, if you want to obtain the number of array elements, you call on the len() function. Likewise, if you want to delete a specific element or range of elements, you call on the del() function.

The second is using an array method. For example, if you want to add elements to an array, you can either use append() or extend(). The difference between the two is that extend() accepts a sequence, so you can add more than one element in a single call. You also have multiple choices when it comes to removing elements. Of course, you can simply delete elements by number, but you can also use pop() to remove the last element in the array. The remove() method actually deletes an element based on value. If an array contains two elements with the same value, remove()deletes only the first of the two elements. IronPython also makes it easy to sort() and reverse() sort the content of an array. Figure 4-6 shows how these various functions and methods affect the test array.

IronPython makes arrays extremely easy to manipulate using functions and methods.

All these methods (and many others) make it possible to use an array in ways that you might not use arrays in other languages. For example, by combining the append() and pop() methods, you can create a stack. If you want a queue, simply use the append() method and del() function. In short, using a combination of functions and methods enables you to work with arrays in ways that would be difficult in other languages.


Posted

in

by

Tags: