我有以下字符串:
"paid,paid,paid,paid,unpaid,paid,paid,paid,paid,unpaid,unpaid,unpaid,paid,paid,Pinterest,unpaid,Pinterest,unpaid,unpaid"
我想把它变成这个:
"paid x4, unpaid x1, paid x4, unpaid x3, paid x2, Pinterest x1, unpaid x1, Pinterest x1, unpaid x2"
基本上,如果下一个项目与前一个项目相同,我想将这些项目组合在一起,并计算其成立次数。因此,在示例中,连续四个未付费转为paid x4
,然后连续一个未付费转为unpaid x1
,然后连续支付的四个转为unpaid x4
,依此类推。
我写了以下功能,我认为可以实现我想要的功能。有更多的pythonic /有效方法吗?
counter = 1
split_string = string_test.split(',')
len_string = len(split_string)
new_string = ''
for num, string in enumerate(split_string):
# Edge case: last string in list
if len_string == num + 1:
if split_string[num] == split_string[num-1]:
new_string += split_string[num] + ' x' + str(counter)
#print("new string:", new_string)
else:
new_string += split_string[num] + ' x1 '
break
else:
if split_string[num] == split_string[num+1]:
counter += 1
#print("string match")
else:
new_string += split_string[num] + ' x' + str(counter) + ', '
#print("new string:", new_string)
counter = 1
print(new_string)
答案 0 :(得分:4)
您可以使用itertools.groupby
在一行(不太复杂)中执行此操作。
>>> from itertools import groupby
>>> ', '.join(['{} x{}'.format(w, len(list(ws))) for w, ws in groupby(string_test.split(','))])
'paid x4, unpaid x1, paid x4, unpaid x3, paid x2, Pinterest x1, unpaid x1, Pinterest x1, unpaid x2'
如果您可以使用它,可以说使用Python 3.6中引入的f-string更清晰一点:
>>> ', '.join([f'{w} x{len(list(ws))}' for w, ws in groupby(string_test.split(','))])