06
Sequence: (three types)
The string cannot be modified.
List [] can be modified ex. [1, 2.3]
Tuple () cannot modify ex. uinfo = ('well, 'male', 20, 'njupt ')
---- Features:
1. indexes can be performed. If the index is negative, the count starts from the right.
2. The Slice operator [m: n] can be used.
---- Basic sequence operation:
1. len ()
2. + # stitching
3. * n # repeat n times
4. in # determine whether the element is in the sequence
5. max () # returns the maximum value.
6. min () # returns the smallest value.
7. cmp (seq1, seq2) # compare whether the two sequence values are the same
07
List: [] ---- variable!
namelist = ['well','tom']nl = namelist ##does not copy the list#Instead, assignment makes the two variables point to the one list in memory.
---- Exclusive operation:
1. list. append # append a value named namelist. append ('Lucy ')
2. del # del namelist [1] delete an element whose list index is 1
3. list. remove # Delete the first matching namelist. remove ('well ')
-------
Tuple (seq) # convert the sequence to tuple
List (seq) # convert a list to list
Creation list:
list = [] ## Start as the empty listlist.append('a') ## Use append() to add elementslist.append('b')
List slice: Same as string slice
list = ['a', 'b', 'c', 'd']print list[1:-1] ## ['b', 'c']list[0:2] = 'z' ## replace ['a', 'b'] with ['z']print list ## ['z', 'c', 'd']
For and in:
For var in list # traverse a list value in collection # test whether a value exists in the Set
Range:
# The range (n) function yields the numbers 0, 1,... n-1,
# And range (a, B) returns a, a + 1,... B-1 -- up to but not including the last number.
While loop:
List common methods:
list.append(elem) #-- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.list.insert(index, elem) #-- inserts the element at the given index, shifting elements to the right.list.extend(list2) #adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().list.index(elem) -- #searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use "in" to check without a ValueError).list.remove(elem) -- #searches for the first instance of the given element and removes it (throws ValueError if not present)list.sort() -- #sorts the list in place (does not return it). (The sorted() function shown below is preferred.)list.reverse() -- #reverses the list in place (does not return it)list.pop(index) -- #removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).
Related exercises:
# C. sort_last
# Given a list of non-empty tuples, return a list sorted in increasing
# Order by the last element in each tuple.
# E.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
# Hint: use a custom key = function to extract the last element form each tuple.
def last(a): return a[-1]def sort_last(tuples): # +++your code here+++ return sorted(tuples,key=last)
# E. Given two lists sorted in increasing order, create and return a merged
# List of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution shoshould work in "linear" time, making a single
# Pass of both lists.
def linear_merge(list1, list2): # +++your code here+++ # LAB(begin solution) result = [] # Look at the two lists so long as both are non-empty. # Take whichever element [0] is smaller. while len(list1) and len(list2):if list1[0] < list2[0]: result.append(list1.pop(0))else: result.append(list2.pop(0)) # Now tack on what's left result.extend(list1) result.extend(list2) return result