简体   繁体   English

计算字符串中字符的出现次数

[英]Counting occurrences of char in string

Hey guys I'm trying to get an output of a4b2c1a2d3 with an input of aaaabbcaaddd .嘿伙计们,我正在尝试使用a4b2c1a2d3的输入获得aaaabbcaaddd的输出。 I think the problem with my code below is the way I implement the counter for previous.我认为我下面的代码的问题是我实现以前的计数器的方式。 Is there an issue with index > 0: ? index > 0:是否有问题?

string1 = "aaaabbcaaddd" 
previous = ""
finalstr = ""
finalint = 1
for index, val in enumerate(string1):
    if index > 0:
        previous = string1[index - 1]
        if previous == val:
            finalint += 1
        else:
            finalstr += previous + str(finalint)
            finalint = 1
print(finalstr)

#outputs "a4b2c1a2"

As I said in the comment, you forgot to add the last count after the loop: 正如我在评论中所说,您忘记在循环添加最后一个计数:

finalstr += previous + str(finalint)

However, unless you are doing this as an assigned homework, there is a much more compact way of solving the problem: 但是,除非您将其作为分配的作业进行,否则,存在解决问题的更紧凑的方法:

from itertools import groupby
''.join(char + str(len(list(group))) for char,group in groupby(string1))
#'a4b2c1a2d3'

Your own code could be rewritten without indexes (they always cause troubles): 您自己的代码可以不用索引而重写(它们总是会引起麻烦):

cnt = 1
finalstr = ''
for x,y in zip(string1, string1[1:]):
    if x==y:
        cnt += 1
    else:
        finalstr += x + (str(cnt) if cnt > 1 else '')
        cnt = 1
finalstr += x + (str(cnt) if cnt > 1 else '')
finalstr
#'a4b2ca2d3'

This is called "run-length encoding." 这称为“行程编码”。 In the Python library more-itertools there's a function to do this encoding: 在Python库more-itertools中,有一个函数可以执行此编码:

>>> from more_itertools import run_length
... 
... string1 = "aaaabbcaaddd"
... list(run_length.encode(string1))
[('a', 4), ('b', 2), ('c', 1), ('a', 2), ('d', 3)]

We can get your desired output by flattening this and string-ifying it. 我们可以通过将其展平并对其进行字符串化来获得所需的输出。

>>> list(flatten(run_length.encode(string1)))
['a', 4, 'b', 2, 'c', 1, 'a', 2, 'd', 3]

>>> list(map(str, flatten(run_length.encode(string1))))
['a', '4', 'b', '2', 'c', '1', 'a', '2', 'd', '3']

>>> ''.join(map(str, flatten(run_length.encode(string1))))
'a4b2c1a2d3'

If anyone was curious of the solution I got round to (based off DYZ's help) 如果有人对解决方案感到好奇,我会去解决这个问题(基于DYZ的帮助)

string1 = "aaaabbcaaddd" 
finalstr = ""
previous = ""
finalint = 0
for c, v in enumerate(string1):
    if c > 0:
        previous = string1[c - 1]
        if previous == v:
            finalint += 1
        else:
            finalstr += previous + str((finalint) if finalint > 1 else '')
            finalint = 1
finalstr += previous + str((finalint) if finalint > 1 else '')
print(finalstr)
#outputs a3b2ca2d3
from itertools import groupby
string1 = "aaaabbcaaddd"
string2 = ''
for c, g in groupby(string1):
    string2 += '%s%d'%(c, len(list(g)))

You could do the same using itertools groupby. 您可以使用itertools groupby进行相同的操作。

from itertools import groupby
string1 = 'aaaabbcaaddd'
result = [[k, len(list(g))] for k, g in groupby(string1)]
final = "".join([i[0]+str(i[1]) for i in result])
final 

output: #'a4b2c1a2d3'

I am new to programming, writing my first comment, i decided this:我是编程新手,写了我的第一条评论,我决定这样做:

string = "qaabbaaccaqwewdssss"
count = 0
temp = str()

for i in range(len(string) - 1):
    count += 1
    if string[i] != string[i + 1]:
        temp += string[i] + str(count)
        count = 0
    elif i + 1 == len(string) - 1:
        count += 1
        temp += string[i] + str(count)
        count = 0

print(temp)
#output: q1a2b2a2c2a1q1w1e1w1d1s4

Simply by using groupby from itertools this can be done.只需使用 itertools 中的 groupby 即可完成。 And there is no need to put everything in one line.并且没有必要将所有内容都放在一行中。

import itertools

s = "aaaabbcaaddd"

iterator = itertools.groupby(s)
f= ""

for key, group in iterator:
    f += str(key) + str(len(list(group)))

print(f)

#output: a4b2c1a2d3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM