python list练习题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python list练习题相关的知识,希望对你有一定的参考价值。
1.合并两个有序列表,并且保持合并后的列表有序 In [20]: lst1 = [32, 42, 12, 5, 14, 4, 1] In [21]: lst2 = [199, 22, 324, 89, 2] In [22]: help(lst1.sort) Help on built-in function sort: sort(...) method of builtins.list instance L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* In [23]: help(lst1.extend) Help on built-in function extend: extend(...) method of builtins.list instance L.extend(iterable) -> None -- extend list by appending elements from the iterable In [27]: #方法一:lst = []lst.extend(lst1)lst.extend(lst2)lst.sort()lst Out[27]: [1, 2, 4, 5, 12, 14, 22, 32, 42, 89, 199, 324] In [38]: lst1 = [32, 42, 12, 5, 14, 4, 1]lst2 = [199, 22, 324, 89, 2] In [35]: lst1.extend(lst2).sort() #lst1.extend(lst2) -> None.sort()lst1 ---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-35-e3d4966adf34> in <module>()----> 1 lst1.extend(lst2).sort() #lst1.extend(lst2) -> None.sort() 2 lst1AttributeError: ‘NoneType‘ object has no attribute ‘sort‘ In [39]: 方法二:lst3 = []lst3 = lst1 + lst2lst3.sort()lst3 Out[39]: [1, 2, 4, 5, 12, 14, 22, 32, 42, 89, 199, 324] In [68]: lst1 = [32, 42, 12, 5, 14, 4, 1]lst2 = [199, 22, 324, 89, 2] In [109]: lst3 = [] #如果为空,enumerate出来的的count也是空,不会抛出异常 In [71]: help(enumerate) Help on class enumerate in module builtins: class enumerate(object) | enumerate(iterable[, start]) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be another object that supports | iteration. The enumerate object yields pairs containing a count (from | start, which defaults to zero) and a value yielded by the iterable argument. | enumerate is useful for obtaining an indexed list: | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. In [72]: for i,j in enumerate(lst1): print(‘{}->{}‘.format(i,j)) 0->32 1->42 2->12 3->5 4->14 5->4 6->1 In [74]: for i,j in enumerate(lst1,start=3): print(‘{}->{}‘.format(i,j)) 3->32 4->42 5->12 6->5 7->14 8->4 9->1 In [84]: en1 = enumerate(lst1,start=3) In [85]: en1 Out[85]: <enumerate at 0x7fc6011736c0> In [87]: en1.__next__() Out[87]: (3, 32) In [88]: en1.__next__() Out[88]: (4, 42) In [89]: next(en1) Out[89]: (5, 12) In [90]: iter(en1) Out[90]: <enumerate at 0x7fc6011736c0> In [96]: en1.__reduce__() Out[96]: (enumerate, (<list_iterator at 0x7fc60116c390>, 6)) In [104]: lsta = [5, 7, 9, 3, 5] In [108]: help(lsta.insert) #在指定的索引前插入指定的对象 Help on built-in function insert: insert(...) method of builtins.list instance L.insert(index, object) -- insert object before index In [106]: lsta.insert(3,88) In [107]: lsta Out[107]: [5, 7, 9, 88, 3, 5] In [1]: lst1 = [32, 42, 12, 5, 14, 4, 1]lst2 = [199, 22, 324, 89, 2] In [2]: lst3 = lst1 + lst2 In [3]: lst3 Out[3]: [32, 42, 12, 5, 14, 4, 1, 199, 22, 324, 89, 2] In [4]: lst4 = []for i in lst3: for n, j in enumerate(lst4): if i < j: lst4.insert(n, i) break else: lst4.append(i) break else: lst4.append(i) In [5]: lst4 #看到问题所在了吗? -> 从14开始出现了问题,原因是,每次循环只和第一个数做了比较 Out[5]: [1, 4, 5, 12, 32, 42, 14, 199, 22, 324, 89, 2] In [17]: #修改之后,将值插入合适的位置#插入排序lst5 = []for i in lst3: for n, j in enumerate(lst5): if i < j: lst5.insert(n, i) break# else:# lst4.append(i)# break else: #为了解决lst5为空的时候,添加第一个值和没有跳出循环(无break)-比lst5里面的数都大,就添加在末尾 lst5.append(i) In [18]: lst5 #成功了 Out[18]: [1, 2, 4, 5, 12, 14, 22, 32, 42, 89, 199, 324] 2.按单词反转字符串 例如‘I love python’ 反转成 ‘python love I’ In [19]: s1 = ‘I love python‘ In [24]: help(reversed) Help on class reversed in module builtins: class reversed(object) | reversed(sequence) -> reverse iterator over values of the sequence | | Return a reverse iterator | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __length_hint__(...) | Private method returning an estimate of len(list(it)). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | __setstate__(...) | Set state information for unpickling. In [25]: for i in reversed(s1): print(i) n o h t y p e v o l I In [30]: s1[::-1] Out[30]: ‘nohtyp evol I‘ In [36]: l1 = s1.split() In [37]: l1 Out[37]: [‘I‘, ‘love‘, ‘python‘] In [40]: l2 = l1[::-1] In [41]: l2 Out[41]: [‘python‘, ‘love‘, ‘I‘] In [46]: s2 = ‘ ‘.join(l2) In [47]: s2 Out[47]: ‘python love I‘ In [48]: s3 = ‘ ‘.join(s1.split()[::-1]) In [49]: s3 Out[49]: ‘python love I‘ 3.找出一个列表中,只出现了一次的数字,并且保持原来的次序,例如[1,2,1,3,2,5],结果为[3,5] In [50]: help(lst1.count) Help on built-in function count: count(...) method of builtins.list instance L.count(value) -> integer -- return number of occurrences of value In [76]: lst1 = [1,2,1,3,2,5] In [71]: lst1.count(1) Out[71]: 2 In [80]: #方法一:效率不高,比如10万个元素的列表,其中9万个是重复的,每个都要count一次,count()了9w此,消耗非常大lst2 = []for x in lst1: if lst1.count(x) == 1: lst2.append(x) In [82]: lst2 Out[82]: [3, 5] In [93]: lst1 = [1,2,1,3,2,5,1] In [87]: #方法二:这个是针对结果列表做操作的,但重复奇数次就不行了ret = []for x in lst1: if x not in ret: ret.append(x) else: ret.remove(x) In [89]: ret Out[89]: [3, 5, 1] In [74]: lst1 Out[74]: [3, 2, 5] In [75]: 3 not in lst1 Out[75]: False In [105]: #方法二修订tmp = []ret = []for x in lst1: if x not in tmp: #tmp存下去重之后的元素 tmp.append(x) ret.append(x) else: #只要在tmp中出现过一次,再一次出现,全部删除,tmp像个 if x in ret: ret.remove(x) In [99]: ret Out[99]: [3, 5] [1,1,1,2,..] | | _ | _ tmp[1,2] | ret[2,] 4.查找一个列表中的最大值 In [100]: lst6 = [128, 99, 100, 23, 54, 443, 118] In [106]: #方法一max(lst6) Out[106]: 443 In [107]: #方法二:排序之后求值|ret = []for x in lst6: for k, y in enumerate(ret): if x > y: ret.insert(k,x) break else: ret.append(x) In [108]: ret Out[108]: [443, 128, 118, 100, 99, 54, 23] In [109]: ret[0] Out[109]: 443 In [110]: #方法三:m = lst6[0]for x in lst6: if x > m: m = x In [111]: m Out[111]: 443 5.写一个程序,把字符串转化为数字,例如‘123’转化为123,‘0.254’转化为0.254,不允许使用int函数,float函数,eval函数,不允许导入任何模块 In [112]: s1 = ‘123.456‘ In [117]: l2 = s1.split(‘.‘) In [118]: l2 Out[118]: [‘123‘, ‘456‘] In [123]: len(l2[0]) Out[123]: 3 In [124]: x, y = [‘1‘,‘3‘] In [125]: x Out[125]: ‘1‘ In [126]: y Out[126]: ‘3‘ In [127]: integer, decimal = s1.split(‘.‘) In [128]: integer Out[128]: ‘123‘ In [129]: decimal Out[129]: ‘456‘ In [130]: length_integer = len(integer) In [131]: length_decimal = len(decimal) In [144]: ret1 = 0for k, x in enumerate(integer): if x == ‘0‘: ret1 += 0 * 10 ** (length_integer - k -1) if x == ‘1‘: ret1 += 1 * 10 ** (length_integer - k -1) if x == ‘2‘: ret1 += 2 * 10 ** (length_integer - k -1) if x == ‘3‘: ret1 += 3 * 10 ** (length_integer - k -1) if x == ‘4‘: ret1 += 4 * 10 ** (length_integer - k -1) if x == ‘5‘: ret1 += 5 * 10 ** (length_integer - k -1) if x == ‘6‘: ret1 += 6 * 10 ** (length_integer - k -1) if x == ‘7‘: ret1 += 7 * 10 ** (length_integer - k -1) if x == ‘8‘: ret1 += 8 * 10 ** (length_integer - k -1) if x == ‘9‘: ret1 += 9 * 10 ** (length_integer - k -1) In [145]: ret1 Out[145]: 123 In [146]: ret2 = 0for k, x in enumerate(decimal): if x == ‘0‘: ret2 += 0 * 10 ** (-1 - k) if x == ‘1‘: ret2 += 1 * 10 ** (-1 - k) if x == ‘2‘: ret2 += 2 * 10 ** (-1 - k) if x == ‘3‘: ret2 += 3 * 10 ** (-1 - k) if x == ‘4‘: ret2 += 4 * 10 ** (-1 - k) if x == ‘5‘: ret2 += 5 * 10 ** (-1 - k) if x == ‘6‘: ret2 += 6 * 10 ** (-1 - k) if x == ‘7‘: ret2 += 7 * 10 ** (-1 - k) if x == ‘8‘: ret2 += 8 * 10 ** (-1 - k) if x == ‘9‘: ret2 += 9 * 10 ** (-1 - k) In [147]: ret2 Out[147]: 0.456 In [148]: ret1 + ret2 Out[148]: 123.456 In [150]: def str2int(strx): integer, decimal = strx.split(‘.‘) length_integer = len(integer) length_decimal = len(decimal) ret1 = 0 for k, x in enumerate(integer): if x == ‘0‘: ret1 += 0 * 10 ** (length_integer - k -1) if x == ‘1‘: ret1 += 1 * 10 ** (length_integer - k -1) if x == ‘2‘: ret1 += 2 * 10 ** (length_integer - k -1) if x == ‘3‘: ret1 += 3 * 10 ** (length_integer - k -1) if x == ‘4‘: ret1 += 4 * 10 ** (length_integer - k -1) if x == ‘5‘: ret1 += 5 * 10 ** (length_integer - k -1) if x == ‘6‘: ret1 += 6 * 10 ** (length_integer - k -1) if x == ‘7‘: ret1 += 7 * 10 ** (length_integer - k -1) if x == ‘8‘: ret1 += 8 * 10 ** (length_integer - k -1) if x == ‘9‘: ret1 += 9 * 10 ** (length_integer - k -1) ret2 = 0 for k, x in enumerate(decimal): if x == ‘0‘: ret2 += 0 * 10 ** (-1 - k) if x == ‘1‘: ret2 += 1 * 10 ** (-1 - k) if x == ‘2‘: ret2 += 2 * 10 ** (-1 - k) if x == ‘3‘: ret2 += 3 * 10 ** (-1 - k) if x == ‘4‘: ret2 += 4 * 10 ** (-1 - k) if x == ‘5‘: ret2 += 5 * 10 ** (-1 - k) if x == ‘6‘: ret2 += 6 * 10 ** (-1 - k) if x == ‘7‘: ret2 += 7 * 10 ** (-1 - k) if x == ‘8‘: ret2 += 8 * 10 ** (-1 - k) if x == ‘9‘: ret2 += 9 * 10 ** (-1 - k) return ret1 + ret2 In [151]: str3 = ‘839.3452‘ In [153]: type(str2int(str3)) Out[153]: float In [154]: str2int(str3) Out[154]: 839.3452 In [ ]:
本文出自 “Zcy.gy” 博客,谢绝转载!
以上是关于python list练习题的主要内容,如果未能解决你的问题,请参考以下文章