1 / 17

COMPSCI 105 Principles of Computer Science

COMPSCI 105 Principles of Computer Science. Lecture 02 - Lists. Lists. Creating a list In memory. my_list = []. my_list. Lists. Adding an element In memory. my_list = my_list + [4]. my_list. 4. Lists. Joining lists In memory. my_list = my_list + [2, 6, 9, 3]. my_list. 4.

chiko
Télécharger la présentation

COMPSCI 105 Principles of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 105Principles of Computer Science Lecture 02 - Lists

  2. Lists • Creating a list • In memory my_list = [] my_list COMPSCI 105 - Principles of Computer Science

  3. Lists • Adding an element • In memory my_list = my_list + [4] my_list 4 COMPSCI 105 - Principles of Computer Science

  4. 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

  5. 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

  6. Exercise • Write a function that called sum_list that sums the elements in a given list COMPSCI 105 - Principles of Computer Science

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

More Related