my_list = [3, 3, 3, 3, 3, 3, 100, 1, 1, 1, 1, 1, 1, 200, 3, 3, 3, 100, 1, 1, 1]
new_list = []
new_list.append(my_list[0]) #adding first element
count = 1 #variable for counting repeated elements
i = 1 #iterator through list
l = len(my_list)
while i < l:
while i < l and my_list[i] == my_list[i-1]:
count +=1
i+=1
if count > 1:
new_list.append(count)
count = 1 #returning to original default value
else:
new_list.append(my_list[i])
i+=1
print(new_list)
od = {}
prev = None
out = []
for ele in l:
if ele != prev and prev in od:
out.extend((prev, od[prev])) if od[prev] > 1 else out.append(prev)
od[prev] = 0
od.setdefault(ele, 0)
od[ele] += 1
prev = ele
out.extend((ele, od[ele])) if od[ele] > 1 else out.append(ele)
print(out)
[3, 6, 100, 1, 6, 200, 3, 3, 100, 1, 3]
In [33]: l = [choice(l) for _ in range(100000)]
In [34]: timeit grp1(l)
10 loops, best of 3: 23.9 ms per loop
In [35]: timeit list(solve(l))
10 loops, best of 3: 33.9 ms per loop
In [36]: list(solve(l)) == grp1(l)
Out[36]: True
不使用itertools的替代解决方案是:
您可以在此处将^{} 与生成器函数一起使用:
itertools是最好的解决方案,但是对于没有导入的另一种方法,我们可以使用dict:
或者在使用更高效的
^{pr2}$defaultdict
的函数中,在dict中存储数据需要更多内存,但使用groupby可能是更快的解决方案:有趣的是,这是一个很好的速度,可能是因为我们不必循环每个子列表来获得len:
相关问题 更多 >
编程相关推荐