第二题:
def sort(nums): for i in range(len(nums) - 1): # 这个循环负责设置冒泡排序进行的次数 for j in range(len(nums) - i - 1): # j为列表下标 if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] return nums print(sort([4,7,3,8,9]))
第三题:
import itertools a1 = 'aabbbccaaa' list1 = [(k, len(list(g))) for k, g in itertools.groupby(a1)] s = '' for i in list1: if i[1] !=1: s += str(i[1]) s += str(i[0]) print(s)
第四题:
import numpy import numpy as np a = np.arange(1,5).reshape(2,2) print('转置前: ',a) print('转置后 ',a.T)
#第五题: list1= [1,2,3,'a','b','c',1,2,'a','b',3,'c','d','a','b',1] set1 = set(list1) result = [(item, list1.count(item)) for item in set1] result.sort(key=lambda x:x[1], reverse=True) print(result)