在 Python 中创建一个压缩函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32855812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a compress function in Python?
提问by Cero
I need to create a function called compress that compresses a string by replacing any repeated letters with a letter and number. My function should return the shortened version of the string. I've been able to count the first character but not any others.
我需要创建一个名为 compress 的函数,它通过用字母和数字替换任何重复的字母来压缩字符串。我的函数应该返回字符串的缩短版本。我已经能够计算第一个字符,但不能计算任何其他字符。
Ex:
前任:
>>> compress("ddaaaff")
'd2a3f2'
def compress(s):
count=0
for i in range(0,len(s)):
if s[i] == s[i-1]:
count += 1
c = s.count(s[i])
return str(s[i]) + str(c)
采纳答案by Patrick Yu
Here is a short python implementation of a compression function:
这是一个压缩函数的简短python实现:
def compress(string):
res = ""
count = 1
#Add in first character
res += string[0]
#Iterate through loop, skipping last one
for i in range(len(string)-1):
if(string[i] == string[i+1]):
count+=1
else:
if(count > 1):
#Ignore if no repeats
res += str(count)
res += string[i+1]
count = 1
#print last one
if(count > 1):
res += str(count)
return res
Here are a few examples:
这里有一些例子:
>>> compress("ddaaaff")
'd2a3f2'
>>> compress("daaaafffyy")
'da4f3y2'
>>> compress("mississippi")
'mis2is2ip2i'
回答by Prune
There are several reasons why this doesn't work. You really need to try debugging this yourself first. Put in a few print statements to trace the execution. For instance:
这不起作用有几个原因。您确实需要先尝试自己调试。放入一些打印语句来跟踪执行。例如:
def compress(s):
count=0
for i in range(0, len(s)):
print "Checking character", i, s[i]
if s[i] == s[i-1]:
count += 1
c = s.count(s[i])
print "Found", s[i], c, "times"
return str(s[i]) + str(c)
print compress("ddaaaff")
Here's the output:
这是输出:
Checking character 0 d
Found d 2 times
Checking character 1 d
Found d 2 times
Checking character 2 a
Found a 3 times
Checking character 3 a
Found a 3 times
Checking character 4 a
Found a 3 times
Checking character 5 f
Found f 2 times
Checking character 6 f
Found f 2 times
f2
Process finished with exit code 0
(1) You throw away the results of all but the last letter's search. (2) You count all occurrences, not merely the consecutive ones. (3) You cast a string to a string -- redundant.
(1) 除了最后一个字母的搜索结果之外,您丢弃了所有结果。(2) 您计算所有出现的次数,而不仅仅是连续出现的次数。(3) 你把一个字符串转换成一个字符串——多余的。
Try working through this example with pencil and paper. Write down the steps youuse, as a human being, to parse the string. Work on translating those to Python.
尝试用铅笔和纸完成这个例子。写下您作为人类解析字符串所使用的步骤。致力于将这些翻译成 Python。
回答by Alexander Fedosov
Short version with generators:
带发电机的简短版本:
from itertools import groupby
import re
def compress(string):
return re.sub(r'(?<![0-9])[1](?![0-9])', '', ''.join('%s%s' % (char, sum(1 for _ in group)) for char, group in groupby(string)))
(1) Grouping by chars with groupby(string)
(1) 按字符分组 groupby(string)
(2) Counting length of group with sum(1 for _ in group)
(because no len
on group is possible)
(2) 计算组的长度sum(1 for _ in group)
(因为没有len
组是可能的)
(3) Joining into proper format
(3) 加入适当的格式
(4) Removing 1
chars for single items when there is a no digit before and after 1
(4) 去掉1
前后无数字的单项字符1
回答by Puneet Chaudhary
x="mississippi"
res = ""
count = 0
while (len(x) > 0):
count = 1
res= ""
for j in range(1, len(x)):
if x[0]==x[j]:
count= count + 1
else:
res = res + x[j]
print(x[0], count, end=" ")
x=res
回答by tanz
input = "mississippi"
count = 1
for i in range(1, len(input) + 1):
if i == len(input):
print(input[i - 1] + str(count), end="")
break
else:
if input[i - 1] == input[i]:
count += 1
else:
print(input[i - 1] + str(count), end="")
count = 1
Output : m1i1s2i1s2i1p2i1
输出:m1i1s2i1s2i1p2i1
回答by Syed Imad
s=input("Enter the string:")
temp={}
result=" "
for x in s:
if x in temp:
temp[x]=temp[x]+1
else:
temp[x]=1
for key,value in temp.items():
result+=str(key)+str(value)
print(result)
打印(结果)
回答by Deb
Just another simplest way to perform this:
执行此操作的另一种最简单方法:
def compress(str1):
output = ''
initial = str1[0]
output = output + initial
count = 1
for item in str1[1:]:
if item == initial:
count = count + 1
else:
if count == 1:
count = ''
output = output + str(count)
count = 1
initial = item
output = output + item
print (output)
Which gives the output as required, examples:
根据需要给出输出,示例:
>> compress("aaaaaaaccddddeehhyiiiuuo")
a7c2d4e2h2yi3u2o
>> compress("lllhhjuuuirrdtt")
l3h2ju3ir2dt
>> compress("mississippi")
mis2is2ip2i
回答by vramazing
Here is something I wrote.
这是我写的东西。
def stringCompression(str1):
counter=0
prevChar = str1[0]
str2=""
charChanged = False
loopCounter = 0
for char in str1:
if(char==prevChar):
counter+=1
charChanged = False
else:
str2 += prevChar + str(counter)
counter=1
prevChar = char
if(loopCounter == len(str1) - 1):
str2 += prevChar + str(counter)
charChanged = True
loopCounter+=1
if(not charChanged):
str2+= prevChar + str(counter)
return str2
Not the best code I guess. But works well.
我猜不是最好的代码。但效果很好。
a -> a1
a -> a1
aaabbbccc -> a3b3c3
aaabbbccc -> a3b3c3
回答by ishmam
This is a solution to the problem. But keep in mind that this method only effectively worksif there's a lot of repetition, specifically if consecutive characters are repetitive. Otherwise, it will only worsenthe situation.
e.g.,
AABCD --> A2B1C1D1
BcDG ---> B1c1D1G1
这是解决问题的方法。但请记住,此方法仅在有大量重复时才有效,特别是在连续字符重复时。否则,只会使情况更加恶化。例如,AABCD --> A2B1C1D1 BcDG ---> B1c1D1G1
def compress_string(s):
result = [""] * len(s)
visited = None
index = 0
count = 1
for c in s:
if c == visited:
count += 1
result[index] = f"{c}{count}"
else:
count = 1
index += 1
result[index] = f"{c}{count}"
visited = c
return "".join(result)
回答by vikashkmr3188
You can simply achieve that by:
您可以简单地通过以下方式实现:
gstr="aaabbccccdddee"
last=gstr[0]
count=0
rstr=""
for i in gstr:
if i==last:
count=count+1
elif i!=last:
rstr=rstr+last+str(count)
count=1
last=i
rstr=rstr+last+str(count)
print ("Required string for given string {} after conversion is {}.".format(gstr,rstr))