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.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
Arrays Chapter 6.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
JavaScript, Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
Matrix Mathematics in MATLAB and Excel
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Fundamentals of Python: First Programs
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lists in Python.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
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.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Input, Output, and Processing
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Lists CS303E: Elements of Computers and Programming.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Working with Collections of Data
Containers and Lists CIS 40 – Introduction to Programming in Python
MATLAB: Structures and File I/O
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Creation, Traversal, Insertion and Removal
CHAPTER THREE Sequences.
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
functions: argument, return value
CISC101 Reminders Assignment 2 due today.
Arrays Arrays A few types Structures of related data items
15-110: Principles of Computing
Introduction to Computer Science
Python List.
Introduction to Computer Science
Presentation transcript:

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 how to extract elements from a list Creating quickly a list of integers using range() Converting between lists and strings Modifying elements in a pre-existing list Checking the contents of lists Python lists present several specificities compared to other programing languages

Lists are variables containing collections of values (strings, numbers, lists or mixtures) Very convenient to handle a large amount of data in a single variable Contrary to dictionaries, lists are ordered: Brackets [] are used both for assignation and index specification: 0 ] - Each element is defined by its position within the list (not by a key) - There cannot be empty positions within list Generalities about Python lists

Indexing lists The first position in a list is always [0], not [1] The first colon « : » within brackets is used to specify a range of position(s) within a list

Indexing lists Negative values are useful to count from the end of lists of unknown size:

Indexing lists A second colon can be used to indicate the step size and the order of the selection:

Unpacking lists i,j = MyList[:2] Assign the first element of MyList to i and the second element to j The number of elements extracted from the list has to be equal to the number of receiving variables

range() function to define a list of integers A third parameter can be used to specify the step size and order of the range: The range is specified using a comma, not a colon The range starts from the first parameter and ends just before the last parameter

Creating a range of letters in alphabetical order ord(‘A’) chr(65) Return 65 Return A Exercise:Print a list of labels corresponding to the wells of a 96-well plate (A1, A2, …, H12) 65 is the ASCII number corresponding to letter A (ord(‘A’),ord(‘I’)) works too

Creating a range of letters in alphabetical order By default, the print command includes an end-of-line character Adding a comma after a print command suppresses this end-of-line Exercise:Try to display the previous list as a real 96 well- plate (with rows and columns)

Comparison between lists and strings Strings behave as lists of characters: Lists can be modified and not strings BUT Both can be combined using +, sorted and iterated within a for loop

Converting between lists and strings String -> List list() function List -> String.join() method.join() is a string method that takes a list argument

Modifying existing lists Adding elements Removing elements Use del() function or reassign an empty list: x = [‘A’,’B’] x[2] = ‘C’ Use.append() method: x.append(‘C’) It is possible to directly insert new elements within a list:

Testing the contents of a list Use the in operator:

Sorting lists.sort() method to sort in place without changing the list sorted() function allows to save the sorted list in a new variable

Identifying unique elements in lists and strings The set() function returns all individual elements contained in a list: The output of the set() function is not a list

List comprehension How can we easily apply a same modification to all elements of a list ? Direct use of operators or methods (such as MyList.upper() ) does not work One solution is to write a for loop through each element of the list:

List comprehension How can we easily apply a same modification to all elements of a list ? A list comprehension is a 1-line for loop specifically designed to modify lists The operation to be done in the loop is written before the for command Useful to extract columns of data from 2D arrays of strings:

List comprehension Many other operations can be done using list comprehension:

Things to know about copying / modifying variables In Python, copying a variable does not create a new variable targeted to a new place in computer memory Instead, it creates a new name that points to the same place as the copied variable in computer memory x=5 y=x x and y point to the same place in memory with value 5 x=8 A new x variable is created with value 8, so y remains unchanged A=[1,2,3] B=A It becomes more tricky with lists that can be modified without creation of a new variable A and B point to the same place in memory containing the list of integers [1,2,3] A[1]=4 Both A and B are changed to [1,4,3] Good way to copy a list: B=A[:] Creates a new variable B at a different place in memory