为什么在某些情况下我的代码将list的打印长度打印为零?

时间:2019-07-16 06:56:35

标签: python python-3.x

我正在开发一个程序,以找出连续字符的数量,并在连续数量大于3时对其进行压缩,如下所示:

如果输入为:

AAAAABBCCCCD

则输出应为

A#5BBC#4D

我已经尝试过:

from itertools import groupby

print(*[x+"#"+str(len(list(c))) for x, c in groupby(input())])

它给了我输出:

A#5 B#2 C#4 D#1

现在,当我尝试在此处输入其他条件时,它将计数打印为零

from itertools import groupby

print(*[x+"#"+str(len(list(c)))  if len(list(c))>3 else list(c) for x, c in groupby(input())])

有人可以告诉我我在做什么错吗?

0 个答案:

没有答案