鱼C论坛

 找回密码
 立即注册
查看: 1886|回复: 7

[已解决]解压的地方没有东西输出呢不明白

[复制链接]
发表于 2023-4-9 15:56:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#利用字符重复出现的次数,编写一个程序,实现基本的字符串压缩功能。
#比如,字符串 FFiiiisshCCCCCC 压缩后变成 F2i4s2h1C6(15字符 -> 10字符,66% 压缩率)。
#所以对于重复次数小于 3 的字符,我们的程序应该选择不对其进行压缩


#压缩字符
text = input('请输入一个字符串')
num=0
text1 = []
text2 = ''
text3 = []
for i in text:
    for j in text:
         if i==j:
             num+=1
    text1.append([i,num])
    num=0
print(text1)
i=0

while i<len(text1):
    if text1[i][1]>3:
        print(text1[i][0],end='')
        text3.append(text1[i][0])
        ''.join((text2,text1[i][0]))
        print(text1[i][1],end='')
        text3.append(text1[i][1])
        ''.join((text2,str(text1[i][1])))
        i=i+text1[i][1]
    else:
        print(text1[i][0],end='')
        text3.append(text1[i][0])
        ''.join((text2,str(text1[i][1])))
        i=i+1


print(text2)
print(text3)

#解压

m = 0
h = 0

for i in text2:
    if text2.isdecimal():
        while m<int(i):
            print(text2[h-1],end='')
        h = h + 1
    else:
        print(i,end='')
        h = h + 1
最佳答案
2023-4-9 16:05:22
本帖最后由 isdkz 于 2023-4-9 16:07 编辑
hcm666 发表于 2023-4-9 16:02
我想知道按我那个代码为啥解压前text2的长度会是0,大佬教教


您好!在您的代码中,text2 的长度为 0 是因为您在将字符追加到 text2 时使用了错误的方法。

您在这段代码中使用了两种方法,但它们都没有正确地修改 text2。以下是您的代码中存在问题的部分:


1、''.join((text2,text1[i][0]))
2、'.join((text2,str(text1[i][1])))


您应该使用 += 运算符来将字符追加到 text2。以下是修改后的代码:
text2 += text1[i][0]
text2 += str(text1[i][1])

不过,您的代码中还存在其他问题。以下是修正这些问题后的完整代码:
# 压缩字符
text = input('请输入一个字符串: ')
num = 0
text1 = []

for i in text:
    if i not in [item[0] for item in text1]:
        num = text.count(i)
        text1.append([i, num])

text2 = ''
i = 0

while i < len(text1):
    if text1[i][1] > 2:
        text2 += text1[i][0]
        text2 += str(text1[i][1])
        i += 1
    else:
        text2 += text1[i][0] * text1[i][1]
        i += 1

print(text2)

# 解压
m = 0
h = 0

while h < len(text2):
    if text2[h].isdigit():
        m = int(text2[h])
        for _ in range(m - 1):
            print(text2[h - 1], end='')
        h += 1
    else:
        print(text2[h], end='')
        h += 1

这段代码首先计算每个字符在字符串中出现的次数,然后根据出现次数来决定是否压缩字符。

注意,这种压缩方法在某些情况下可能会导致压缩后的字符串比原始字符串更长,例如当输入为 "aabbcc" 时。最后,使用一个简单的循环来解压缩字符串。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-9 15:57:21 | 显示全部楼层
本帖最后由 isdkz 于 2023-4-9 15:58 编辑

您的代码中有一些问题,让我帮您修正这些问题并提供一个有效的解决方案。
def compress_string(text):
    compressed = []
    count = 1

    for i in range(1, len(text)):
        if text[i] == text[i - 1]:
            count += 1
        else:
            if count < 3:
                compressed.extend([text[i - 1]] * count)
            else:
                compressed.append(text[i - 1])
                compressed.append(str(count))
            count = 1

    if count < 3:
        compressed.extend([text[-1]] * count)
    else:
        compressed.append(text[-1])
        compressed.append(str(count))

    return ''.join(compressed)


# 压缩字符
text = input('请输入一个字符串: ')
compressed_text = compress_string(text)
print(f"压缩后的字符串: {compressed_text}")

# 解压字符
def decompress_string(compressed_text):
    decompressed = []
    i = 0

    while i < len(compressed_text):
        if compressed_text[i].isdigit():
            count = int(compressed_text[i])
            decompressed.extend([compressed_text[i - 1]] * count)
            i += 1
        else:
            if not (i + 1 < len(compressed_text) and compressed_text[i + 1].isdigit()):
                decompressed.append(compressed_text[i])
            i += 1

    return ''.join(decompressed)


decompressed_text = decompress_string(compressed_text)
print(f"解压后的字符串: {decompressed_text}")

这个解决方案中,首先定义了一个名为compress_string的函数,用于压缩输入的字符串。

接着,定义了一个名为decompress_string的函数,用于解压缩压缩过的字符串。

最后,我们调用这两个函数来压缩和解压缩用户输入的字符串。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 15:57:52 | 显示全部楼层
占楼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-4-9 16:02:55 | 显示全部楼层
isdkz 发表于 2023-4-9 15:57
您的代码中有一些问题,让我帮您修正这些问题并提供一个有效的解决方案。

我想知道按我那个代码为啥解压前text2的长度会是0,大佬教教
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 16:05:05 | 显示全部楼层
hcm666 发表于 2023-4-9 16:02
我想知道按我那个代码为啥解压前text2的长度会是0,大佬教教


根据给出的代码,发现 `text2` 长度为 0 的原因是在压缩的过程中,虽然有一行代码 `''.join((text2,str(text1[i][1])))`,但并没有将新的字符串添加到 `text2` 中。这是因为在 python 中,字符串对象是不可变的,每次对字符串做修改时,会生成一个新的字符串对象。所以需要将这句代码改成
 `text2 = ''.join((text2,str(text1[i][1])))
`,即将新的字符串对象赋值给 `text2` 变量,才能实现将数字字符串拼接起来。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 16:05:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-4-9 16:07 编辑
hcm666 发表于 2023-4-9 16:02
我想知道按我那个代码为啥解压前text2的长度会是0,大佬教教


您好!在您的代码中,text2 的长度为 0 是因为您在将字符追加到 text2 时使用了错误的方法。

您在这段代码中使用了两种方法,但它们都没有正确地修改 text2。以下是您的代码中存在问题的部分:


1、''.join((text2,text1[i][0]))
2、'.join((text2,str(text1[i][1])))


您应该使用 += 运算符来将字符追加到 text2。以下是修改后的代码:
text2 += text1[i][0]
text2 += str(text1[i][1])

不过,您的代码中还存在其他问题。以下是修正这些问题后的完整代码:
# 压缩字符
text = input('请输入一个字符串: ')
num = 0
text1 = []

for i in text:
    if i not in [item[0] for item in text1]:
        num = text.count(i)
        text1.append([i, num])

text2 = ''
i = 0

while i < len(text1):
    if text1[i][1] > 2:
        text2 += text1[i][0]
        text2 += str(text1[i][1])
        i += 1
    else:
        text2 += text1[i][0] * text1[i][1]
        i += 1

print(text2)

# 解压
m = 0
h = 0

while h < len(text2):
    if text2[h].isdigit():
        m = int(text2[h])
        for _ in range(m - 1):
            print(text2[h - 1], end='')
        h += 1
    else:
        print(text2[h], end='')
        h += 1

这段代码首先计算每个字符在字符串中出现的次数,然后根据出现次数来决定是否压缩字符。

注意,这种压缩方法在某些情况下可能会导致压缩后的字符串比原始字符串更长,例如当输入为 "aabbcc" 时。最后,使用一个简单的循环来解压缩字符串。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-4-9 16:10:43 | 显示全部楼层
sfqxx 发表于 2023-4-9 16:05
根据给出的代码,发现 `text2` 长度为 0 的原因是在压缩的过程中,虽然有一行代码 `''.join((text2,str ...

注意整理chatgpt格式哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 16:17:04 From FishC Mobile | 显示全部楼层
歌者文明清理员 发表于 2023-4-9 16:10
注意整理chatgpt格式哈

ok
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 21:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表