Array

Using the range() Function

Must Read

Admin
Admin
Just post!

The range() function is one of the more interesting functions when it comes to arrays because you can use it in so many ways. The basic range() function outputs a series of numbers beginning with 0 and ending with one less than the number you specify. For example, if you specify range(10), you get the numbers from 0 through 9. Listing 4-8 shows some examples of using the range() function.

Listin g 4-8: Using the range() function

[code]

# Display a simple range.
print(‘Simple Range:’)
for x in range(10):
print(x),
# Display a specific range.
print(‘nnRange from 5 to 10’)
for x in range(5, 11):
print(x),
# Display the even numbers from 2 through 10.
print(‘nnEven Numbers 2 Through 10:’)
for x in range(2, 11, 2):
print(x),
# Create an array.
MyList = [‘Red’, ‘Blue’, ‘Green’, ‘Yellow’]
# Use the range() and len() functions to iterate the array.
print(‘nnIterating an Array’)
for Index in range(len(MyList)):
print MyList[Index]
# Pause after the debug session.
raw_input(‘nPress any key to continue…’)
[/code]

As previously mentioned, the basic range() function outputs numbers starting from 0 through one less than the number you specify. Consequently, if you want to display the range of numbers from 5 through 10, you must actually specify a range of 5 through 11. Notice how you separate a range using a comma. The first number is the starting point and the second is the ending point. You may choose any starting and ending point — even negative numbers.

The range() function also supports a step as a third argument. Consequently, you can output the even numbers from 2 through 10 by specifying the correct range and using a step of 2.

Of course, using range() by itself has significant limitations. What you really want to do is use range() with arrays to make it easier to display the array content. The next part of the example shows how to perform this task. Notice how this example uses the len() function to obtain an upper limit for the range() function. Figure 4-7 shows the output from this example.

Use the range() function to generate sequences of numbers for array and other processing.

Now that you know about using range() and len() together, let’s look at an example of ragged array processing. The following code shows how to process a ragged array.

[code]

# Create a ragged array.
MyList = [[1, 2], 3, 4, [5, 6]]
# Display the array content. In this case, you must place the ragged
# array portion of the example in a try block so that the code
# displays just the first dimension when only one dimension is
# available.
print(‘nDisplaying the ragged array.’)
print(‘X Y Value’)
for x in range(len(MyList)):
try:
for y in range(len(MyList[x])):
print x, y, ‘ ‘, MyList[x][y]
except TypeError:
print x, ‘N/A’, MyList[x]

[/code]

In this case, you begin with a ragged array that contains the numbers 1 through 6. In order to process this kind of array, you begin with the first array dimension, which will always have either a sub-array or a value. In this case, there are four array elements — two sub-arrays and two values (3 and 4).

Of course, you want the values in those sub-arrays. Consequently, the next step is to place the subarray processing in a try…except block because the processing will fail when the code encounters a value. The moment the code tries to get the length of the sub-array using len(), it will fail with a TypeError. When the error does occur, the example prints just the first dimension. You can use this pattern for any ragged array you need to process. Figure 4-8 shows the output from this example.

Ragged arrays are relatively easy to process using range() and len().

When working with IronPython, there are typically multiple ways to perform the same task. Many developers (with good reason) won’t process data by exception. Fortunately, there’s another way to process a ragged array that doesn’t involve the try…except block shown earlier. Here’s the second method.

[code]

# Create a ragged array.
MyList = [[1, 2], 3, 4, [5, 6]]
# Display the array content.
print(‘nDisplaying the ragged array.’)
print(‘X Y Value’)
for x in range(len(MyList)):
if type(MyList[x]).__name__ == ‘list’:
for y in range(len(MyList[x])):
print x, y, ‘ ‘, MyList[x][y]
else:
print x, ‘N/A’, MyList[x]

[/code]

In this case, the code simply checks the type of the MyList element before it performs any additional processing on it. Notice that this technique relies on the type() function, which returns the actual type of the element, and then you obtain the string form of the type using the __name__ attribute. The results are the same as shown in Figure 4-8.

- Advertisement -

Latest News

Elevate Your Bentley Experience: The Bespoke Elegance of Bentayga EWB by Mulliner

Bentley Motors redefines the essence of bespoke luxury with the introduction of the Bentayga EWB's groundbreaking two-tone customization option—a...
- Advertisement -

More Articles Like This

- Advertisement -