Google python course basic exercise——list1
学习完string之后,现在来看看list的用法:
1 # -*- coding: cp936 -*- 2 #A. match_ends 3 # Given a list of strings, return the count of the number of 4 # strings where the string length is 2 or more and the first 5 # and last chars of the string are the same. 6 def match_ends(words): 7 count = 0 8 for item in words: 9 if len(item)>=2 and item[0] == item[-1]: 10 count += 1 11 return count 12 13 # B. front_x 14 # Given a list of strings, return a list with the strings 15 # in sorted order, except group all the strings that begin with 'x' first. 16 # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields 17 # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] 18 def front_x(words): #思路为将以'x'开头的和剩下的,分开作为两个list分别排序 19 list_1 = [] 20 list_2 = [] 21 for item in words: 22 if item[0] == 'x': 23 list_1.append(item) 24 else: 25 list_2.append(item) 26 list_1.sort() 27 list_2.sort() 28 return list_1+list_2 #此处常见错误为return list_1.extend(list_2),返回值为None 29 30 # C. sort_last 31 # Given a list of non-empty tuples, return a list sorted in increasing 32 # order by the last element in each tuple. 33 # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields 34 # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] 35 def sort_last(tuples): #采用关键字思想,先对最后一个元素排序,然后用匹配的方法把原tuples里的元素对应排序 36 list_1 = [] 37 list_2 = [] 38 for tup in tuples: 39 key = tup[-1] 40 list_1.append(key) 41 list_1.sort() 42 for num in range(len(tuples)): 43 for tup in tuples: 44 if tup[-1] == list_1[num]: 45 list_2.append(tup) 46 return list_2 47 48 # Simple provided test() function used in main() to print 49 # what each function returns vs. what it's supposed to return. 50 def test(got,expected): 51 if got == expected: 52 prefix = 'OK' 53 else: 54 prefix = 'X' 55 print '%s got: %s expected: %s ' % (prefix, repr(got),repr(expected)) 56 57 # Calls the above functions with interesting inputs. 58 def main(): 59 print 'match_ends' 60 test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3) 61 test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2) 62 test(match_ends(['aaa', 'be', 'abc', 'hello']), 1) 63 64 print 65 print 'front_x' 66 test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']), 67 ['xaa', 'xzz', 'axx', 'bbb', 'ccc']) 68 test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']), 69 ['xaa', 'xcc', 'aaa', 'bbb', 'ccc']) 70 test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']), 71 ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']) 72 73 print 74 print 'sort_last' 75 test(sort_last([(1, 3), (3, 2), (2, 1)]), 76 [(2, 1), (3, 2), (1, 3)]) 77 test(sort_last([(2, 3), (1, 2), (3, 1)]), 78 [(3, 1), (1, 2), (2, 3)]) 79 test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]), 80 [(2, 2), (1, 3), (3, 4, 5), (1, 7)]) 81 if __name__ == '__main__': 82 main()
运行结果:
match_ends OK got: 3 expected: 3 OK got: 2 expected: 2 OK got: 1 expected: 1 front_x OK got: ['xaa', 'xzz', 'axx', 'bbb', 'ccc'] expected: ['xaa', 'xzz', 'axx', 'bbb', 'ccc'] OK got: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'] expected: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'] OK got: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] expected: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] sort_last OK got: [(2, 1), (3, 2), (1, 3)] expected: [(2, 1), (3, 2), (1, 3)] OK got: [(3, 1), (1, 2), (2, 3)] expected: [(3, 1), (1, 2), (2, 3)] OK got: [(2, 2), (1, 3), (3, 4, 5), (1, 7)] expected: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
ps:
- 在学完list和string后,容易发现他俩的异同之处:
list[0:2] = 'z'
string[0:2] = 'z'
前者能实现替换功能,而后者会报错。即list支持item assignment 但string不支持
2.对于list methods而言,调用函数不会返回新list,只会在原来的list上修改。
3.python中in语句,返回Ture/False,但像find之类的函数,当在搜索范围内未找到对象时,会抛出异常如ValueError。
补充:这两天无意中又翻了下sort的用法,顿时对C题有了新的认识。另一种代码:
1 def sort_last(tuples): 2 def get_last(tup): 3 return tup[-1] 4 return sorted(tuples,key=get_last)
不得不说key=功能太好用了!一般而言,sort()只针对list使用,但sorted()却没有这一限制,它适用于许多可列项形式的类型,所以推荐使用sorted()函数。

浙公网安备 33010602011771号