Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

enumerate() is useful when you want to keep a running count while looping through items using a for loop:

Code Block

mylist = ["a", "b", "c"]
for i as int, obj in enumerate(mylist):
      print i, ":", obj

...

Taking an enumerable object such as a list or a collection, it returns an IEnumerable object that applies "function" to each element in the array.

Examples:

Code Block

def HardRock(item):
	return "$item totally rocks out, man!"

wyclefSongs = ("Two wrongs", "Dirty Dancing")

x = map(wyclefSongs, HardRock)
for y in x:
	print y

//another example using a multiline anonymous closure:
newlist = map([1,2,3,4,5,6]) def (x as int):
    return x*x*x

Output:

Code Block

Two wrongs totally rocks out, man!
Dirty Dancing totally rocks out, man!

...

Creates a multidimensional array of type elementType with the specifications of length.

Examples

Code Block

foo = matrix(int, 2, 2)
foo[0, 0] = 0
foo[0, 1] = 1
foo[1, 0] = 2
foo[1, 1] = 3

print join(foo) //prints "0, 1, 2, 3"
/* Looks like,
[0, 1
 2, 3]
*/

...

Returns an IEnumerable object that contains elements from 0 to max - 1.

Code Block

#This prints 0 through 9:
for i in range(10):
    print i

...

Returns an IEnumerable object that contains all of the elements from begin to end - 1 that match the interval of step.

Example:

Code Block

for i in range(0, 10, 2):
	print i

Output:

Code Block

0
2
4
6
8

reversed

reversed(enumerable as object) as IEnumerable

...

zip will return an IEnumerable object, wherein each element of the IEnumerable object will be a one dimensional array containing two elements; the first element will be an element located in "first" and the second will be an element located in "second."

Example:

Code Block

firstNames = ("Charles", "Joe", "P")
lastNames = ("Whittaker", "Manson", "Diddy")

x = zip(firstNames, lastNames)
for y in x:
	print join(y)
        //print y[0], y[1]

Output:

Code Block

Charles Whittaker
Joe Manson
P Diddy