鱼C论坛

 找回密码
 立即注册
查看: 2432|回复: 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。以下是修改后的代码:

  1. text2 += text1[i][0]
  2. text2 += str(text1[i][1])
复制代码


不过,您的代码中还存在其他问题。以下是修正这些问题后的完整代码:

  1. # 压缩字符
  2. text = input('请输入一个字符串: ')
  3. num = 0
  4. text1 = []

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

  9. text2 = ''
  10. i = 0

  11. while i < len(text1):
  12.     if text1[i][1] > 2:
  13.         text2 += text1[i][0]
  14.         text2 += str(text1[i][1])
  15.         i += 1
  16.     else:
  17.         text2 += text1[i][0] * text1[i][1]
  18.         i += 1

  19. print(text2)

  20. # 解压
  21. m = 0
  22. h = 0

  23. while h < len(text2):
  24.     if text2[h].isdigit():
  25.         m = int(text2[h])
  26.         for _ in range(m - 1):
  27.             print(text2[h - 1], end='')
  28.         h += 1
  29.     else:
  30.         print(text2[h], end='')
  31.         h += 1
复制代码


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

注意,这种压缩方法在某些情况下可能会导致压缩后的字符串比原始字符串更长,例如当输入为 "aabbcc" 时。最后,使用一个简单的循环来解压缩字符串。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

您的代码中有一些问题,让我帮您修正这些问题并提供一个有效的解决方案。

  1. def compress_string(text):
  2.     compressed = []
  3.     count = 1

  4.     for i in range(1, len(text)):
  5.         if text[i] == text[i - 1]:
  6.             count += 1
  7.         else:
  8.             if count < 3:
  9.                 compressed.extend([text[i - 1]] * count)
  10.             else:
  11.                 compressed.append(text[i - 1])
  12.                 compressed.append(str(count))
  13.             count = 1

  14.     if count < 3:
  15.         compressed.extend([text[-1]] * count)
  16.     else:
  17.         compressed.append(text[-1])
  18.         compressed.append(str(count))

  19.     return ''.join(compressed)


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

  24. # 解压字符
  25. def decompress_string(compressed_text):
  26.     decompressed = []
  27.     i = 0

  28.     while i < len(compressed_text):
  29.         if compressed_text[i].isdigit():
  30.             count = int(compressed_text[i])
  31.             decompressed.extend([compressed_text[i - 1]] * count)
  32.             i += 1
  33.         else:
  34.             if not (i + 1 < len(compressed_text) and compressed_text[i + 1].isdigit()):
  35.                 decompressed.append(compressed_text[i])
  36.             i += 1

  37.     return ''.join(decompressed)


  38. decompressed_text = decompress_string(compressed_text)
  39. print(f"解压后的字符串: {decompressed_text}")
复制代码


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

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

最后,我们调用这两个函数来压缩和解压缩用户输入的字符串。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-9 15:57:52 | 显示全部楼层
占楼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

我想知道按我那个代码为啥解压前text2的长度会是0,大佬教教
小甲鱼最新课程 -> https://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 中,字符串对象是不可变的,每次对字符串做修改时,会生成一个新的字符串对象。所以需要将这句代码改成
  1. `text2 = ''.join((text2,str(text1[i][1])))
复制代码
`,即将新的字符串对象赋值给 `text2` 变量,才能实现将数字字符串拼接起来。
小甲鱼最新课程 -> https://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。以下是修改后的代码:

  1. text2 += text1[i][0]
  2. text2 += str(text1[i][1])
复制代码


不过,您的代码中还存在其他问题。以下是修正这些问题后的完整代码:

  1. # 压缩字符
  2. text = input('请输入一个字符串: ')
  3. num = 0
  4. text1 = []

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

  9. text2 = ''
  10. i = 0

  11. while i < len(text1):
  12.     if text1[i][1] > 2:
  13.         text2 += text1[i][0]
  14.         text2 += str(text1[i][1])
  15.         i += 1
  16.     else:
  17.         text2 += text1[i][0] * text1[i][1]
  18.         i += 1

  19. print(text2)

  20. # 解压
  21. m = 0
  22. h = 0

  23. while h < len(text2):
  24.     if text2[h].isdigit():
  25.         m = int(text2[h])
  26.         for _ in range(m - 1):
  27.             print(text2[h - 1], end='')
  28.         h += 1
  29.     else:
  30.         print(text2[h], end='')
  31.         h += 1
复制代码


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

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

使用道具 举报

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

注意整理chatgpt格式哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

ok
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 10:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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