Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Course A201: Introduction to Programming 10/28/2010.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
More Strings CS303E: Elements of Computers and Programming.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Python Exceptions and bug handling Peter Wad Sackett.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Python Basics Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Reasons to learn Python 1.Short development time 2.Learning curve.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
Python Pattern Matching and Regular Expressions Peter Wad Sackett.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Perl Variables: Array Web Programming.
Lists in Python.
Bryan Burlingame Halloween 2018
Guide to Programming with Python
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Data Structures: Lists
Topics Sequences Introduction to Lists List Slicing
CS 1111 Introduction to Programming Fall 2018
Python Lists and Sequences
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Data Structures: Lists
Python Data Structures: Lists
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Python Review
CSE 231 Lab 6.
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Introduction to Computer Science
Presentation transcript:

Python Lists and Sequences Peter Wad Sackett

2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable ordered sequence of objects Changable Sequence, shares properties with strings Any objects, like a mix of strings, numbers, etc. A simple list assignment: primes = list() primes = [2, 3, 5, 7, 11, 13, 17] print (primes[2]) Result is 5 as lists are zero-based like strings. Assignment of an element in a list – only in range. primes[5] = 12

3DTU Systems Biology, Technical University of Denmark Similarities between lists and strings Strings and lists are both sequences - they work much the same. primes = [2, 3, 5, 7, 11, 13] Length numberofprimes = len(primes) Addition and multiplication numbers = primes + [1, 4, 6, 8] numbers = 2 * primes A slice of a list partprimes = primes[2:4] Advanced slicing everysecondprime = primes[::2]

4DTU Systems Biology, Technical University of Denmark Changing lists A slice assignment of a list can be thought of as a two step process: Deletion – the slice specified to the left of = is deleted. Insertion – the new list is inserted where the element where deleted. primes = [1, 2, 3, 5, 8, 9, 10, 13, 19] Deletion of an element primes [0:1] = [] Inserting element primes[-1:-1] = [17] Replacing elements primes[3:6] = [7, 11] Slice assignment

5DTU Systems Biology, Technical University of Denmark List methods Many of these methods are similar to slice assignment primes = [2, 3, 5, 7, 11, 13] Adding one element to the end of the list primes.append(17) Adding several elements (as list) to the end of the list primes.extend([19, 23, 29]) Inserting into the list at index position. 4 is inserted here primes.insert(2, 4) Deleting an element or a slice del primes[2] Popping an element, default last element elem = primes.pop()# removing 13 elem = primes.pop(1) # removing 3 Remove the first element from list with this value primes.remove(11) Clearing a list primes.clear()

6DTU Systems Biology, Technical University of Denmark More complex list methods Reversing a list in place primes.reverse() Sort a list in place, very powerful, costly, smallest element at top primes.sort() All elements have to be of the same type to be compared properly The sort order can be reversed, smallest element at button primes.sort(reverse=True) All elements from the list can be assigned a sort-key, and they will be sorted according to that key instead of their own value. words = [’I’, ’am’, ’Peter’, ’the’, ’Boogyman’] Sorting above, but ignoring case words.sort(key=str.lower) key has to be assigned a function that takes one argument and returns a value The built-in function sorted() is similar, but returns a new list

7DTU Systems Biology, Technical University of Denmark Splitting Splitting a string into a list sentence = ’Let there be light’ words = sentence.split() Split with no arguments splits on whitespace, ignoring leading and trailing whitespace. Very often used. You can also split on specific character(s) commadata = ’Peter,172,78’ mylist = commadata.split(sep=’,’) You can decide to split only at the first separator mylist commadata(sep=’,’,maxsplit=1) Any split results always in a list

8DTU Systems Biology, Technical University of Denmark Joining Joining a list of strings into a string, the opposite of split fairytale = [’Snowwhite’, ’and’, ’the’, ’seven’, ’dwarves’] mystring = ’-’.join(fairytale) Result is ’Snowwhite-and-the-seven-dwarves’ The string (here ’-’) providing the method is the string that joins the elements of the list. Popular choices for strings are ’\n’ and ’ ’. The methods split() and join() are both string methods, but works with lists.

9DTU Systems Biology, Technical University of Denmark Copying lists primes = [2, 3, 5, 7, 11, 13] Lists are python objects. When assigning one list to a new variable, you are just making a new variable that referes to the same list. Any change made to primes will be made to samelist and vice versa. samelist = primes samelist += [17] print(primes) Result: [2, 3, 5, 7, 11, 13, 17] To make a new different list with the same content as the old do differentlist = primes[:] This is called a deep copy as opposed to the above shallow copy. This has been the source of many errors.

10DTU Systems Biology, Technical University of Denmark Command line arguments By importing the sys library, you gain access to sys.argv, the argument vector. It is a list that contains the arguments to the program. The first element in the list is the program name and the following are the arguments. The program ’mynameis.py’ consist of these statements. import sys print(’Your name is’, sys.argv[1]) The result of running the command ’mynameis.py Peter’ is: Your name is Peter

11DTU Systems Biology, Technical University of Denmark Better program import sys if len(sys.argv) == 1: print(”You did not supply a name”) else: name = ’ ’.join(sys.argv[1:]) print(’Your name is’, name)

12DTU Systems Biology, Technical University of Denmark Tubles Tubles are immutable lists Supports all list methods that does not change the list Assigning tubles, use parenthesis mytub = ()# empty tuble mytub = (3,)# one element mytub = (2, 3, 5, 7)# more elements Why use tubles when we have lists? The immutable property provides integrity Uses less memory Performance There is more to tubles and lists than this