我需要编写一个接收str的函数,并给出每个字符在序列中连续出现的次数。字符串以列表形式表示 列表中的每个元素也是一个列表,包含2个元素: 一个字符及其重复次数。 例如:
input: 'aaabbcb'
output: [[a,3],[b,2][c,1][b,1]]
代码需要是一个简单的函数,因为我是python的新手
def str2rle (n):
n=[n]
counter=1
for char in n:
if len(n)==1:
return [char,1]
else:
if char[0]==char[1]:# I know this row isn't right but I cant think of the right row to #write
counter+=1
return [char,counter]
return [char,1]
答案 0 :(得分:1)
您可以在此处使用列表列表:
def str2rle(text):
ans = []
prev = None #initialize `prev` to None
#iterate over the string
for c in text:
# if current character is not equal to the previous character
# then append a new list
if c != prev:
ans.append([c, 1]) #insert new list at the end
prev = c #set `prev` to current character
else:
# if current character is equal to the prev then simply increment
# the second item of the last list
ans[-1][1] += 1 #ans[-1] return the last list from `ans`
return ans
<强>演示:强>
>>> str2rle('aaabbcb')
[['a', 3], ['b', 2], ['c', 1], ['b', 1]]
>>> str2rle('foobar')
[['f', 1], ['o', 2], ['b', 1], ['a', 1], ['r', 1]]
一种pythonic和有效的方法是使用itertools.groupby
:
>>> from itertools import groupby
>>> [[k, sum(1 for _ in g)] for k, g in groupby('aaabbcd')]
[['a', 3], ['b', 2], ['c', 1], ['d', 1]]
答案 1 :(得分:1)
递归解决方案
s = 'aaabbcb'
def sc(s,t=s[0],c=0, ans=[]):
if len(s)>0:
if s[0] == t:
return sc(s[1:],s[0],c+1, ans)
else:
return sc(s[1:],s[0],1, ans +[[t,c]])
else:
return ans + [[t,c]]
print sc(s)
[['a', 3], ['b', 2], ['c', 1], ['b', 1]]
编辑:不可变的默认参数
s = 'aaabbcb'
def sc(s,ans,t,c):
if len(s)>0:
if s[0] == t:
return sc(s[1:], ans ,s[0],c+1)
else:
return sc(s[1:],ans +[[t,c]],s[0],1)
else:
return ans + [[t,c]]
print sc(s,[],s[0],0)
编辑:我想说的是,我想
def str2l(s):
def sc(s,t,ans,c = 0):
if len(s)>0:
if s[0] == t:
return sc(s[1:],s[0], ans,c+1)
else:
return sc(s[1:],s[0], ans +[[t,c]],1)
else:
return ans + [[t,c]]
return sc(s,s[0],[])
print str2l('aaabbcb')
答案 2 :(得分:0)
因此,请在代码中查看我的评论以查看答案。
import unittest
from itertools import cycle
def count_n_times_letter_appears(word):
lst_times = []
lst_cycle = cycle(word)
# get first item
next_item = lst_cycle.next()
count_times = 1
idx = 0
# append the first letter to the list
lst_times.append([word[0], count_times])
for letter in word[:-1]:
# counter for each time a repeated letter appears in a row
current_item, next_item = next_item, lst_cycle.next()
# if it is the same item as before, increment
if current_item == next_item:
count_times += 1
lst_times[idx] = [current_item, count_times]
# create a new index to append a new letter/counter
else:
count_times = 1
lst_times.append([next_item, count_times])
idx += 1
return lst_times
class TestRepeatingLetters(unittest.TestCase):
def test_count_times_appears_your_example(self):
self.assertEqual([['a', 3], ['b', 2], ['c', 1], ['b', 1]], count_n_times_letter_appears("aaabbcb"))
def test_count_common_word(self):
self.assertEqual([['g', 1], ['o', 2], ['d', 1]], count_n_times_letter_appears("good"))
def test_count_no_repeated_letters(self):
self.assertEqual([['a', 1], ['b', 1], ['c', 1]], count_n_times_letter_appears("abc"))
if __name__ == "__main__":
unittest.main()