标签:... rev bsp lis exce else 函数 code pre
1. 写一个函数, 输入一个字符串, 返回倒序排列的结果: 如: string_reverse(‘abcdef’),
返回: ‘fedcba’ (请采用多种方法实现)
s = ‘abcdef‘ l = list(s) l.sort(reverse=True) s = ‘‘.join(l) print(s) s = ‘abcdef‘ s1 = ‘‘ for i in s[::-1]: s1 += i print(s1) s = ‘abcdef‘ l = [] [l.append(i) for i in s[::-1]] s = ‘‘.join(l) print(s)
2. 请用自己的算法, 合并如下两个list按大小排序, 并去除重复的元素:
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
l = [] list1 = [2, 3, 8, 4, 9, 5, 6] list2 = [5, 6, 10, 17, 11, 2] l = list1 + list2 print(list(set(l))) list1 = [2, 3, 8, 4, 9, 5, 6] list2 = [5, 6, 10, 17, 11, 2] [list1.append(i) for i in list2 if i not in list1] print(list1) l = [2, 3, 8, 4, 9, 5, 6, 10, 17, 11] print(sorted(l)) l = [2, 3, 8, 4, 9, 5, 6, 10, 17, 11] l.sort() print(l)
3.python 如何捕获异常
def num(s): try: for i in s: print(i) except Exception as e: print(e) finally: print(‘执行完毕‘) num(‘kkk‘) def num1(s): if type(s) == int: raise ValueError(‘s is not int‘) else: for i in s: print(i) try: num1(‘wang‘) except ValueError as e: raise TypeError(‘type error‘) def num2(s): assert type(s) != int, ‘s is not int....‘ for i in s: print(i) num2(‘wang‘)
标签:... rev bsp lis exce else 函数 code pre
原文地址:https://www.cnblogs.com/wangshijie95/p/10173571.html