Introduction of tuple, lambda and map

We start with Command:EditPythonScript and later we will switch the environment to GhPython.

1. Tuple

Tuple is an object which is very similar to list type objects.
However, you CANNOT append a new item to a tuple.
You CANNOT remove an item from a tuple.
You CANNOT insert an item to a tuple.

Because of this, tuple is used to represent a tiny structure.

a=[1,2,3] #list
b=(1,2,2) #tuple
print type(a)
print type(b)
a.append(4)
print a
b.append(4)
print b

You may get
|<type 'list'>
|
|<type 'tuple'>
|
|Runtime error (MissingMemberException): 'tuple' object has no attribute 'append'
|Traceback:
| line 6, in script

You can access to the items by using a[index]

a=(1,2,3)
print a[0]
print a[1]
print a[2]

You may get
|1
|
|2
|
|3
You can also use tuple in a tuple (nested tuple)
a=((1,2,3),2)
print a
print a[0]
print a[1]
print a[0][0]
print a[0][1]
print a[0][2]

|((1, 2, 3), 2)
|
|(1, 2, 3)
|
|2
|
|1
|
|2
|
|3

2. A tuple containing a single item

Let's try

print (1)
print ((1))
print (((1)))

You may get
|1
|
|1
|
|1
This mean that, (a) doesn't mean a tuple containing a single item but 'a' itself.
In order to create a tuple that contains a single item, you need to type (a,)
print (1,)
print ((1,),)
print (((1,),),)

You may get
|(1,)
|
|((1,),)
|
|(((1,),),)

3. Unpacking tuple

Unpacking a tuple makes your codes readable.

a=(1,2,3)
(x,y,z)=a
print x
print y
print z

You may get
|1
|
|2
|
|3

You can also unpack nested tuple

a=((1,2,3),3)
(center,R)=a
((x,y,z),R)=a
print center
print x
print y
print z
print R

|(1, 2, 3)
|
|1
|
|2
|
|3
|
|3

4. tuples in a list

Without unpacking, a code would look like

import rhinoscriptsyntax as rs
centers=[(1,1,0),(2,4,0),(3,9,0),(4,16,0)]
a=list()
for center in centers:
    a.append(rs.AddPoint(el))

By using unpacking, your code would become more readable, such as..

import rhinoscriptsyntax as rs
centers=[(1,1,0),(2,4,0),(3,9,0),(4,16,0)]
a=list()
for center in centers:
    (x,y,z)=center
    a.append(rs.AddPoint(x,y,z))

or simply,

import rhinoscriptsyntax as rs
centers=[(1,1,0),(2,4,0),(3,9,0),(4,16,0)]
a=list()
for (x,y,z) in centers:
    a.append(rs.AddPoint(x,y,z))
1.png

5. nested tuple in a list

import rhinoscriptsyntax as rs
circles=[((0,0,0),1.0),((1,0,0),2.0),((2,0,0),4.0)]
a=list()
for circle in circles:
    a.append(rs.AddCircle(circle[0],circle[1]))

2.png
You can use unpacking to make your code readable.
import rhinoscriptsyntax as rs
circleList=[((0,0,0),1.0),((1,0,0),2.0),((2,0,0),4.0)]
a=list()
for circle in circleList:
    ((x,y,z),R)=circle
    a. append (rs. AddCircle ((x,y,z), R))

or simply
import rhinoscriptsyntax as rs
circleList=[((0,0,0),1.0),((1,0,0),2.0),((2,0,0),4.0)]
a=list()
for ((x,y,z),R) in circleList:
    a. append (rs. AddCircle ((x,y,z), R))

6. zipping two lists

Sometimes, zip() makes your code more readable.
If you have two lists such as
a=[a1,a2,a3,a4] and
b=[b1,b2,b3,b4],
zip(a,b) returns
[(a1,b1),(a2,b2),(a3,b3),(a4,b4)]

Without using zip(), your code would look like

import rhinoscriptsyntax as rs
centerList=[(0,0,0),(1,0,0),(2,0,0)]
radiusList=[1.0,2.0,4.0]
a=list()
for i in range(3):
    a.append(centerList[i],radiusList[i])

But, if you use zip(), you code will be
import rhinoscriptsyntax as rs
circleList=[(0,0,0),(1,0,0),(2,0,0)]
radiusList=[1.0,2.0,4.0]
a=list()
for (center,radius) in zip(circleList,radiusList):
    a.append(rs.AddCircle(center,radius))

7. Lambda functions

A "lambda input:output(input)" syntax returns a function.
Since this function has no name, it is called anonymous function.
The anonymous functions are so-called lambda function.

F=lambda x:x**2
print(F(1))
print(F(2))
print(F(3))

You may get
|1
|4
|9

Note taht, in Python, xy returns x to the power of y. (Often it is expressed as x^y in other numerical environments)

8. A lambda function can return a tuple

A lambda function can return only single object. However, since tuple is treated as a single object, you can return a set of values by using tuple.
For example,

F=lambda x:(x,x**2,0)
print(F(1))
print(F(2))
print(F(3))

You may get
|(1, 1, 0)
|(2, 4, 0)
|(3, 9, 0)

9. map

The map(F,x) function applies a lambda function F to the items of the list x by one-by one rule.
For example, F(x)=x*x will project [1,2,3] to [1,4,9]
1 2 3
| | |
1 4 9
This projection can be computed by

F=lambda x:(x,x**2,0)
x=range(1,4)
y=map(F,x)
print(y)

Again, you may get
|[(1, 1, 0), (2, 4, 0), (3, 9, 0)]
**Note that this is a single list that contains three tuples.

10. Nested map

import rhinoscriptsyntax as rs
F1=lambda x:(x,x**2,0)
F2=lambda x:rs.AddPoint(x)
x=range(1,4)
a=map(F2,map(F1,x))

Of course, you can always use "for" syntax.
map() is provides an alternative way of generating a new list by using for syntax.
import rhinoscriptsyntax as rs
a=list()
for i in range(1,4):
    a.append(rs.AddPoint((i,i**2,0)))

8.png