Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Python Lists and Sequences Peter Wad Sackett

2 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

3 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]

4 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

5 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()

6 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

7 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

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

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

10 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

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

12 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


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

Similar presentations


Ads by Google