Download presentation
Presentation is loading. Please wait.
1
CSE 231 Lab 6
2
Topics to cover Creating Lists & tuples Lists methods
In-place vs not in-place Patterns List comprehension A Python list is a sequence of objects. It is a sequence like a string. It shares characteristics such as indexing & slicing, iteration (for) and membership (in) as well as functions such as len().
3
LISTS and tuples A sequence of objects.
It is a sequence like a string. It shares characteristics such as indexing & slicing, iteration (for) and membership (in) as well as functions such as len(). Mutable vs Immutable Strings, ints, floats, tuples are all immutable Lists are Mutable Tuples are immutable lists
4
Lists my_list = ["Arthur", "King", "of", "the", "Britons"]
5
Lists – Creating Lists my_list = [] # start off with nothing in the list my_list = ['I', 'am', 'Full', 'of', 'stuff'] my_list = list(‘CSE231 Rocks') # using list() ['C', 'S', 'E', '2', '3', '1', ' ', 'R', 'o', 'c', 'k', 's'] mixed_list = [“string”, 1, 42.0, True] Matrix = [[1, 1], [1,1]]
6
List methods: In-place
my_list.append('A') # takes element arg. & puts to end of list my_list.pop(3) # takes index arg. & takes it out of list If no argument is given, it pops out the last element What does this return? my_list.sort() # sorts entire list & modifies it in place. What does the function sorted(my_list) do? my_list.insert(index, value) # Insert value into position index my_list.remove(value) # removes the first instance of value # crashes if the value doesn't exist
7
Lists – What is in-place?
some_list = [] some_list.append(1) print(some_list) >>> [1] # Why didn’t I need to do some_list = some_list.append(1) ? # Methods (like append) on lists happen IN-PLACE meaning there is no return!
8
Lists – What is in-place?
>>> some_list = [3, 1, 7, 2, 4] >>> some_list = some_list.sort() >>> print(some_list) None # uh oh. What happened to the data??? # You reassigned some_list when sort is done in-place! # The return of a function that does not return something is what?
9
Lists – What is in-place?
>>> some_list = [3, 1, 7, 2, 4] >>> some_list.sort() >>> print(some_list) [1, 2, 3, 4, 7] # All good again!
10
List methods: NOT-IN-PLACE
‘-’.join(my_list) # joins entire list with a delimiter ‘-’ >>my_list = [‘I’, ’am’, ‘Full’, ‘of’, ‘stuff’] >>my_string= ‘-’.join(my_list) >>print(my_string) I-am-Full-of-stuff
11
Split Method >>my_str='I am in my 231 lab‘
split and also strip methods are your best friends with data parsing By default it separates strings by whitespaces into a list Very helpful when given lines of data. >>my_str='I am in my 231 lab‘ >>my_str.split() ['I', 'am', 'in', 'my', '231', 'lab'] >>my_str.split('m') ['I a', ' in ', 'y 231 lab']
12
Patterns Counter: x = 0 # initialize at zero
x += 1 # increment the counter Strings: s = ‘’ # initialize with empty string s += ch # concatenate characters Lists: L = [] # initialize with empty list L.append(value) # append values to the list
13
List Comprehensions L=[] For i in range(3)
L.append(i**3) L = [i**3 for i in range(3)] Other examples >> L = [i**3 for i in range(10) if i%2 == 0] >>L = [int(x) for x in line_list] line = “ ” # original data read from a file line_list = line.split() #split to get a list of strings, SHOULD CONVERT TO INT NEXT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.