Python快速入门(3)列表、练习题
程序员文章站
2022-07-07 07:59:40
06
序列:(三种类型)
字符串 不可以修改
列表list [] 可以修改 ex.[1,2.3]
元组tuple () 不可以修改 ex. uinfo = ('we...
06
序列:(三种类型)
字符串 不可以修改
列表list [] 可以修改 ex.[1,2.3]
元组tuple () 不可以修改 ex. uinfo = ('well,'male',20,'njupt')
----特点:
1.可以进行索引,索引为负数,则从右边开始计数
2.可以使用切片操作符 [m:n]
----基本序列操作:
1. len()
2. + #拼接
3. *n #重复n次
4. in #判断元素是否在序列中
5. max() #返回最大的值
6. min() #返回最小的值
7. cmp(seq1,seq2) #比较2个序列值是否相同
07
列表list:[] ---- 可变的!
----专属操作:
1. list.append #追加一个值 namelist.append('lucy')
2. del #del namelist[1] 删除列表索引为1的元素
3. list.remove #删除第一个匹配性 namelist.remove('well')
-------
tuple(seq) #将序列转换为tuple
list(seq) #将列表转换为list
创建列表:
列表切片:同字符串切片一样
for 和 in :
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循环:
相关练习:
# 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 should work in "linear" time, making a single
# pass of both lists.
序列:(三种类型)
字符串 不可以修改
列表list [] 可以修改 ex.[1,2.3]
元组tuple () 不可以修改 ex. uinfo = ('well,'male',20,'njupt')
----特点:
1.可以进行索引,索引为负数,则从右边开始计数
2.可以使用切片操作符 [m:n]
----基本序列操作:
1. len()
2. + #拼接
3. *n #重复n次
4. in #判断元素是否在序列中
5. max() #返回最大的值
6. min() #返回最小的值
7. cmp(seq1,seq2) #比较2个序列值是否相同
07
列表list:[] ---- 可变的!
namelist = ['well','tom'] nl = namelist ##does not copy the list #instead, assignment makes the two variables point to the one list in memory.
----专属操作:
1. list.append #追加一个值 namelist.append('lucy')
2. del #del namelist[1] 删除列表索引为1的元素
3. list.remove #删除第一个匹配性 namelist.remove('well')
-------
tuple(seq) #将序列转换为tuple
list(seq) #将列表转换为list
创建列表:
list = [] ## start as the empty list list.append('a') ## use append() to add elements list.append('b')
列表切片:同字符串切片一样
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 和 in :
for var in list #遍历一个列表 value in collection #测试集合中是否存在一个值
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循环:
list常用方法:
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()).
相关练习:
# 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 should 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]