/* */

Tuesday, 22 March 2011

Using 'key' in sort

x = ['add', 'acme', 'aerate', 'aardvark', 'abalone']
x.sort(key = len)
x
['add', 'acme', 'aerate', 'abalone', 'aardvark']

This example uses 'len' as the key function - it arranges the items in order of their length.

Sorting tutorial online:

http://wiki.python.org/moin/HowTo/Sorting 

Sorted

x = [4,6,2,1,7,9]
y= sorted(x)
x
[4,6,2,1,7,9]
y
[1,2,4,6,7,9]

Sorted function very similar to 'sort'. Can also be used on a sequence:

sorted('Python')
['P,' 'h', 'o', 'n', 't', 'y']

Arranges the string into an alphabetical sequence.

Sort

Sort a list into numerical order - this changes the original rather than returning a copy of the list;

x = [4,6,2,1,7,9]
x.sort()
x
[1,2,4,6,7,9]

Reverse

reverse elements in a list;

x = [1,2,3]
x.reverse()
x
[3,2,1]

This reverses the list but doesn't return anything.

Remove

removes 1st occurance of a value in a list;

x = ['to', 'be', 'or', 'not', 'to', 'be']
x.remove('be')
x
['to', 'or', 'not','to', 'be' ]

Pop

x= [1,2,3,]
x.pop()
3

Deletes the last entry of the list

x
[1,2]
x.pop(0) - specifies which item to delete
1 - returns the value of the item it just deleted
x
[2]

Insert

insert an object into specified position in a list:

numbers [1,2,3,4,,5,6]
numbers.insert(3,'four') - insert 'four' at position 3
number
[1,2,3,'four',5,6,7]

Index

Knights = ['Richard', 'of ', 'York', 'Gave', 'Battle', 'in', 'vein']
Knights.index('York')
2
Knights.index('pudding')
-error

Finds the word in the list and returns its position.

Count

['to','be','or','not','to','be'].count ('to')
2

This counts the amount of times a given object appears in the list (in this case the word 'to')

Extend

append several items at once:

a = [1,2,3]
b = [4,5,6]
a.extend(b)
a
[1,2,3,4,5,6]

This is similar to concatenation except the variable 'a' is modified. In concatenation, the result would create a new variable.

Append

lst = [1,2,3,]
lst.append(4)
lst
[1,2,3,4]

Simply adds the value in the brackets to the variable (note the variable was declared 'lst' and not 'list'. 'list' is a function and would have given an error).

Remove elements

numbers = [1,2,3,4,5,]
numbers [1,4] = []
numbers = [1,5]

This has similar effect to the delete statement and is related to replace. In this case you are simply replacing all the elements from position 1 up to (but not including) position 4 and replacing it with [] - an empty list. The items that survive in the list are those in position [0,4]. 

Insert elements

numbers = [1,5]
numbers [1:1] = [2,3,4]
numbers
[1,2,3,4,5,]

The inserted elements are placed in the list at position 1

List statement, assign to slices

name = list('Pearl')
name
['P','e','a,'r','l'']
name[2:] = list('ar')
name
['P','e','a','r']

List statement turned the string ('Pearl') into a list.
The list('ar') replaces anything from position 2 onwards in the name list, hence it's new value being ['P','e','a','r']

Deletion

names = ['Alice', 'Beth', 'Julie', 'Cecil', 'Gary']
del names [2]
names
['Alice', 'Beth', 'Cecil', 'Gary']

Use the del function to delete item from list.

Changing Lists

- assignment:
x = [1,1,1] - Declare list and its values
x[1] = 2 - Assign value of 2 to position 1
x
[1,2,1,] - New value of x

To change list items, index the item you want to change (x[1]) and assign a new value to it (x[1] =2).

Len Min/Max

example:

numbers = [100, 34, 678]
len(numbers)
3

max(numbers)
678

min(numbers)
34

max(2,3)
3

min(2,3)

Len function returns either the highest or lowest values in a sequence (the first two examples) or directly as a sequence argument (the last two examples).