170 likes | 285 Vues
This lecture explores the fundamental concepts of lists in computer science, covering topics such as list creation, adding and accessing elements, joining lists, and advanced operations like list comprehensions. We discuss functions for summing elements, checking for palindromes, and manipulating list structures in memory. Additionally, you will learn about list characteristics, including storage efficiency and element access. Join us as we unravel the essential principles for effective list usage in programming.
E N D
COMPSCI 105Principles of Computer Science Lecture 02 - Lists
Lists • Creating a list • In memory my_list = [] my_list COMPSCI 105 - Principles of Computer Science
Lists • Adding an element • In memory my_list = my_list + [4] my_list 4 COMPSCI 105 - Principles of Computer Science
Lists • Joining lists • In memory my_list = my_list + [2, 6, 9, 3] my_list 4 2 6 9 3 COMPSCI 105 - Principles of Computer Science
Lists • Accessing an element of the list • In memory print(my_list[3]) my_list 4 2 6 9 3 2 3 0 1 4 COMPSCI 105 - Principles of Computer Science
Exercise • Write a function that called sum_list that sums the elements in a given list COMPSCI 105 - Principles of Computer Science
Functions of lists • append(item) • insert(i, item) • pop() • pop(i) • sort() • reverse() • del( my_list[i] ) • index(item) • count(item) • remove(item) COMPSCI 105 - Principles of Computer Science
Exercise • 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 COMPSCI 105 - Principles of Computer Science
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]] 1 2 3 2 0 1 4 5 6 2 0 1 7 0 8 9 0 1 my_list 2 3 0 1 COMPSCI 105 - Principles of Computer Science
Exercise • Draw a diagram showing how the following list is structured in memory: my_list = [[1, 2], [[3, 4], [5, 6, 7]]] COMPSCI 105 - Principles of Computer Science
List comprehensions • A list can be created using instructions that appear within the square brackets • The general format is as follows: [expression for variable in sequence] my_list = [x for x in range(0, 100)] COMPSCI 105 - Principles of Computer Science
Examples • To convert a string to a list of characters: • To generate all the even numbers between 1 and 100 my_list = [c for c in 'Andrew'] my_list = [n * 2 for n in range(1, 50)] COMPSCI 105 - Principles of Computer Science
Exercise • Write a list comprehension that generates all the odd numbers between 1 and 50 (inclusive) • Given a list containing strings as follows: • days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] • Use a list comprehension to generate a list containing the reverse of each string. • rdays = COMPSCI 105 - Principles of Computer Science
Exercise • Generating a list of random numbers • Given that a random number between X and Y inclusive can be obtained using the code: • import random • number = random.randint(X, Y) • Write a list comprehension to generate a list of 100 random values between 0 and 1 to simulate tossing a coin. COMPSCI 105 - Principles of Computer Science
List comprehensions that use conditions • We can extend the syntax for a list comprehension to include a condition: • The general format is as follows: [expression for variable in sequence if condition] my_list = [x for x in range(0, 100) if x % 2 == 0] COMPSCI 105 - Principles of Computer Science
Examples • Generate a list of the non-vowel letters that appear in a string: • name = 'Andrew Luxton-Reilly' • vowels = 'aeiou' my_list = [c for c in name if c not in vowels] COMPSCI 105 - Principles of Computer Science
Features of lists • 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) COMPSCI 105 - Principles of Computer Science