Python Data Structures

Содержание

Слайд 2

Lists An ordered group of items Does not need to be

Lists

An ordered group of items
Does not need to be the same

type
Could put numbers, strings or donkeys in the same list
List notation
A = [1,”This is a list”, c, Donkey(“kong”)]
Слайд 3

Methods of Lists List.append(x) adds an item to the end of

Methods of Lists

List.append(x)
adds an item to the end of

the list
List.extend(L)
Extend the list by appending all in the given list L
List.insert(I,x)
Inserts an item at index I
List.remove(x)
Removes the first item from the list whose value is x
Слайд 4

Examples of other methods a = [66.25, 333, 333, 1, 1234.5]

Examples of other methods

a = [66.25, 333, 333, 1, 1234.5] //Defines

List
print a.count(333), a.count(66.25), a.count('x') //calls method
2 1 0 //output
a.index(333)
//Returns the first index where the given value appears
1 //ouput
a.reverse() //Reverses order of list
a //Prints list a
[333, 1234.5, 1, 333, -1, 66.25] //Ouput
a.sort()
a //Prints list a
[-1, 1, 66.25, 333, 333, 1234.5] //Output
Слайд 5

Using Lists as Stacks The last element added is the first

Using Lists as Stacks

The last element added is the first element

retrieved
To add an item to the stack, append() must be used
stack = [3, 4, 5]
stack.append(6)
Stack is now [3, 4, 5, 6]
To retrieve an item from the top of the stack, pop must be used
Stack.pop()
6 is output
Stack is now [3, 4, 5] again
Слайд 6

Using Lists as Queues First element added is the first element

Using Lists as Queues

First element added is the first element retrieved
To

do this collections.deque must be implemented
Слайд 7

List Programming Tools Filter(function, sequence) Returns a sequence consisting of the

List Programming Tools

Filter(function, sequence)
Returns a sequence consisting of the items from

the sequence for which function(item) is true
Computes primes up to 25
Слайд 8

Map Function Map(function, sequence) Calls function(item) for each of the sequence’s

Map Function

Map(function, sequence)
Calls function(item) for each of the sequence’s items
Computes the

cube for the range of 1 to 11
Слайд 9

Reduce Function Reduce(function, sequence) Returns a single value constructed by calling

Reduce Function

Reduce(function, sequence)
Returns a single value constructed by calling the binary

function (function)
Computes the sum of the numbers 1 to 10
Слайд 10

The del statement A specific index or range can be deleted

The del statement

A specific index or range can be deleted

Слайд 11

Tuples Tuple A number of values separated by commas Immutable Cannot

Tuples

Tuple
A number of values separated by commas
Immutable
Cannot assign values to individual

items of a tuple
However tuples can contain mutable objects such as lists
Single items must be defined using a comma
Singleton = ‘hello’,
Слайд 12

Sets An unordered collection with no duplicate elements Basket = [‘apple’,

Sets

An unordered collection with no duplicate elements
Basket = [‘apple’, ‘orange’, ‘apple’,

‘pear’]
Fruit = set(basket)
Fruit
Set([‘orange’, ‘apple’, ‘pear’])
Слайд 13

Dictionaries Indexed by keys This can be any immutable type (strings,

Dictionaries

Indexed by keys
This can be any immutable type (strings, numbers…)
Tuples can

be used if they contain only immutable objects
Слайд 14

Looping Techniques Iteritems(): for retrieving key and values through a dictionary

Looping Techniques

Iteritems():
for retrieving key and values through a dictionary

Слайд 15

Looping Techniques Enumerate(): for the position index and values in a sequence

Looping Techniques

Enumerate():
for the position index and values in a sequence

Слайд 16

Zip(): for looping over two or more sequences

Zip():
for looping over two or more sequences