一道经典的python题目【多测师_王sir】
#coding=utf-8 """ =========================== Author:多测师_王sir Time:2020-11-22 14:50 Wechat:xiaoshubass website:www.duoceshi.cn =========================== """ ''' 题目:
python的列表里相邻一样的元素组成一个列表,[1,1,3,6,6,7,7,7,8,5,5,5] 最后要变成[[1,1],[3],[6,6],[7,7,7],[8],[5,5,5]] ''' def duoceshi(num_list): result_list =[] for i in range(0,len(num_list)): new_list = [] if num_list: value1 = num_list.pop(0) new_list.append(value1) for j in range(0,len(num_list)): if num_list[0] == value1: value2 =num_list.pop(0) new_list.append(value2) result_list.append(new_list) print(result_list) duoceshi([1, 1, 3, 6, 6, 7, 7, 7, 8, 5, 5, 5])