Lists COMPSCI 105 S2 2015 Principles of Computer Science.

16
Lists COMPSCI 105 S2 2015 Principles of Computer Science

Transcript of Lists COMPSCI 105 S2 2015 Principles of Computer Science.

Page 1: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

Lists

COMPSCI 105 S2 2015Principles of Computer Science

Page 2: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1052

Lists Lists are a built-in type in Python

Use square brackets to signify a list Lists can contain any type of data, or any mixture of

datamy_list1 = [1, 2, 3]

my_list2 = ['Hello', 'Is', 'there', 'anybody', 'out', 'there?']

my_list3 = [1, 5.899, 'Hello']

my_list4 = [4, 2, 6, 9, 3]

my_list 4 2 6 9 3

Lecture 02

Page 3: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1053

List functions Numerous list functions are supported

Use help(list) to find out the functions Examples:

>>> x = [1, 2, 3]>>> len(x)

Lecture 02

3

[1, 2, 3, 4]

[1, 2, 3, 5]>>> x += [5]

>>> x + [4]

>>> 3 in xTrue

1>>> x[0]

x 1 2 3

x 1 2 3 5

Page 4: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

append(item) insert(i, item) pop() pop(i) sort() reverse() del( my_list[i] ) index(item) count(item) remove(item)

Functions of lists

4COMPSCI 105

Lecture 02

Page 5: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

Write a function that determines if a list is a palindrome.

Your function should return true if the contents of the list are the same regardless of whether the elements are accessed in forward or reverse order.

>>> is_palindrome([1, 2, 3, 2, 1]) True

Exercise

5COMPSCI 105

Lecture 02

Page 6: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1056

Lists of lists Since a list can contain anything, it can of course

contain a list

In memory

my_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]

my_list0 1 2 3

1 2 30 1 2

4 5 60 1 2

70

8 90 1

Lecture 02

Page 7: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1057

Exercise 4 Draw a diagram showing how the following list is

structured in memory:

my_list = [[1, 2], [[3, 4], [5, 6, 7]]]

Lecture 02

Page 8: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1058

Slices of sequences A piece of a sequence can be obtained using the

following syntax sequence_name[x:y] where x is the index of the first element and y is the

index after the last element

>>> name = 'Andrew'>>> name[0:0]

Lecture 02

''

'A'

'ndr'

>>> name[0:1]

>>> name[1:4]

Page 9: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 1059

Slice step valueActually, the syntax allows for a third value, used to define the step size between elements included in the slice. If a value if omitted, it defaults to [start:end:1]

If the step size is negative, it starts at the end and steps backward towards the start.

>>> name = 'Andrew'>>> name[:4:2]

>>> name = 'Andrew'>>> name[::-1]

name A n d r e w

Positive Index

0 1 2 3 4 5

Negative index

-6 -5 -4 -3 -2 -1

Lecture 02

'ad'

'werdnA'

Page 10: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10510

For loops Used to iterate through a sequence

numbers = [2, 3, 5, 7, 11]for i in numbers: print(i)

name = "Andrew"for c in name: print(c)

Lecture 02

235711

Andrew

Example01.py

Page 11: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10511

While loops Used to execute code when the end condition is

unknownname = "Andrew Luxton-Reilly"i = 0while name[i] != ' ': i += 1print('Space is at position:', i)

Space is at position: 6

Lecture 02

Example01.py

Page 12: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10512

Range Range is a special object in Python

Used to generate integer numbers within a range of values

Can iterate through the rangefor x in range(0, 5): print(x)

Lecture 02

01234

Example01.py

Page 13: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10513

List comprehensions A list can be created using instructions that

appear within the square brackets

The general format is as follows:

Examples: To convert a string to a list of characters:

To generate all the even numbers between 1 and 20

my_list = [x for x in range(0, 10)]

[expression for variable in sequence]

my_list = [c for c in 'Andrew']

my_list = [n * 2 for n in range(1, 10)]

Lecture 02

Page 14: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10514

Exercise 5 Write a list comprehension that generates all the

odd numbers between 1 and 50

Lecture 02

Page 15: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10515

We can extend the syntax for a list comprehension to include a condition:

The general format is as follows:

Examples: Generate a list of the non-vowel letters that appear in

a string:

List comprehensions that use conditions

my_list = [x for x in range(0, 10) if x % 2 == 0]

[expression for variable in sequence if condition]

Lecture 02

>>> name = 'Andrew Luxton-Reilly'>>> vowels = 'aeiou‘>>> my_list = [c for c in name if c not in vowels]

['A', 'n', 'd', 'r', 'w', ' ', 'L', 'x', 't', 'n', '-', 'R', 'l', 'l', 'y']

Page 16: Lists COMPSCI 105 S2 2015 Principles of Computer Science.

COMPSCI 10516

Information in a list is stored contiguously in memory location of the information can be calculated location = start of the list + index * size of each

element

Efficiency issues It takes the same time to access any of the elements Slow to move elements around (i.e. add and delete

elements from within the list)

Features of lists

Lecture 02