如何在列表中合并相似的字符?

10

我正在尝试将列表中相邻的相似字符组合在一起。 我想知道是否有一种Python方法可以实现? 这里是一个示例:

test = 'hello###_world###test#test123##'
splitter = re.split("(#)", test)
splitter = filter(None, splitter)

这将在分割器变量中返回此内容:

['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#']
我试图合并哈希值,以便列表变成这样:
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']

感谢任何帮助!


如果您想要将相似的字符组合在一起,您的结果必须是 ['h', 'e', 'll', 'o', '###'...]。在您的情况下,您可以通过重复 # 来拆分字符串。 - Mykola Zotko
re.split使用正确的模式或itertools.groupby通常可以实现此功能。 - pylang
4个回答

9

尝试:

splitter = re.split("(#+)", test)

1
它在结尾处返回一个空字符串。 - Sociopath
3
使用splitter = filter(None, splitter)(与原帖相同)时,它不会这样做。 - DYZ
没错。我通常会养成使用原始字符串与正则表达式模式的习惯,例如 r"(#+)" - pylang

6
您可以使用 itertools.groupby
import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]

输出:

['hello', '###', '_world', '###', 'test', '#', 'test123', '##']

您也可以使用re.findall
import re
result = re.findall('#+|[^#]+', test)

输出:

['hello', '###', '_world', '###', 'test', '#', 'test123', '##']

这个也可以完成任务。感谢帮助! - Greg

3
在正则表达式的末尾添加 + ,然后过滤掉 None 值就可以解决问题。
>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>

0
如果你想将列表中每个相似的字符组合在一起。例如:['h', 'eee', 'lll', 'oo', '#########', '_', 'w', 'r', 'd', 'tttt', 'ss', '1', '2', '3'],你可以使用以下代码实现。
def combine(s):
dict={}
for i in s:
    key = dict.keys()
    if i in key:
        dict[i]+=1
    else:
        dict[i]=1
v,k = list(dict.values()),list(dict.keys())
product =[]
for i,j in zip(v,k):
    product.append(i*j)
print(product)`enter code here 
s = 'hello###_world###test#test123##'    
count(s)

输出: ['h', 'eee', 'lll', 'oo', '#########', '_', 'w', 'r', 'd', 'tttt', 'ss', '1', '2', '3']


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接