2

假设我要求输入并且用户输入:

"RED REDD REDDD REDDDDD"

我将字符串转换为一个列表,名为red. red[0] = 'RED', red[1] = 'REDD'等等。

我如何得到它,以便它只显示 letter D,并删除其他所有内容?So then red[0]would be D, red[1]would beDD等等。有办法吗?谢谢

4

3 回答 3

3

您可以使用re.sub; 'D'替换除''.

>>> import re
>>> strs = "RED REDD REDDD REDDDDD ABCDeeeeDDDDDDDD ABCD"
>>> [re.sub(r'[^D]+', r'', item) for item in strs.split()]
['D', 'DD', 'DDD', 'DDDDD', 'DDDDDDDDD', 'D']

或者:

>>> re.sub(r'[^D\s]+', r'', strs).split()
['D', 'DD', 'DDD', 'DDDDD', 'DDDDDDDDD', 'D']
于 2013-09-07T04:32:11.927 回答
3

您有很多选择,其中一些已经在其他答案中注明,还有这些:

用于模式匹配的正则表达式

import re
s = "RED REDD REDDD REDDDDD"
pattern = r'D+' 
re.findall(pattern, s)

字符串操作

[''.join([c for c in x if c=='D']) for x in s.split()]
于 2013-09-07T04:36:49.483 回答
1

我的解决方案可能看起来不像上面的那样可读性和直接性,但它们展示了一些替代方法,这些方法可能会让你对你首先尝试做的事情有不同的看法。至少,这些与使用正则表达式或列表推导的不同之处在于它们应该很好地处理大量输入,仅在您要求时生成您想要的组。

itertools.groupby

from itertools import groupby
# Groups of adjacent letters 'D' and not 'D'
g = groupby("RED REDD REDDD REDDDD", lambda c:c=='D')
# Literally what you asked for:
[''.join(i[1]) for i in g if i[0]]

集合.计数器

from collections import Counter
words = "RED REDD REDDD REDDDD".split()
# for large input: (i[itertools.groupby("RED REDD REDDD REDDDD", lambda c:c!=' '))
counters = (Counter(w) for w in words)
# Literally what you asked for:
[c['D']*'D' for c in counters]
于 2013-09-07T05:11:10.620 回答