3
ss = ''
for word in ['this','his','is','s']: # Attach the words
    if word not in ss:                          # if they are not already present
       ss = ss + word + ' '                     # to ss(substring) AFTER ss.

它给出的输出为:

'this '

但我想得到:

'this his is s '

如何使用'in'关键字来做到这一点?

4

4 回答 4

9
ss = []
for word in ['this', 'his', 'is', 's']:
    if word not in ss:
        ss.append(word)
ss = ' '.join(ss)
于 2013-08-14T06:13:08.403 回答
6

使用集合,您的代码的问题是所有['his','is','s']实际上都是 的子字符串'this',因此条件始终为 False。(in查找子字符串。)

>>> 'his' in 'this'
True
>>> 'is' in 'this'
True
>>> 's' in 'this'
True

解决方案1:

>>> seen = set()     #keep a track of seen word here.
>>> words = ['this','his','is','s']
>>> output = []
>>> for word in words:
...     if word not in seen:
...         output.append(word)
...         seen.add(word)
...         
>>> print " ".join(output) #This is better than normal string concatenation
this his is s

使用列表推导的上述代码的较小版本:

>>> seen = set() 
>>> " ".join([x for x in words if x not in seen and not seen.add(x)])
'this his is s'

解决方案2:

另一种方法(仅用于学习目的)是使用带有单词边界的正则表达式:

>>> import re
>>> ss = ''
for word in words:
    #now this regex looks for exact word match, not just substring         
    if not re.search(r'\b{}\b'.format(re.escape(word)), ss): 
        ss += word + ' '
...         
>>> ss
'this his is s '
于 2013-08-14T06:11:25.127 回答
0

正如所有其他答案中所解释的那样,问题是后面的“单词”都是第一个的子字符串,但是我想说问题是您正在比较粉笔和奶酪,或者在您的情况下,单词与字符串进行比较 - 如果您将单词与单词进行比较,问题就消失了:

>>> ss = ''
>>> for word in ['this','his','is','s']: # Attach the words
...     if word not in ss.split():       # if they are not already present in the list of words so far
...        ss = ss + word + ' '          # to ss(substring) AFTER ss.
...
>>> ss
'this his is s '

这正是你所要求的。

于 2013-08-14T07:31:51.163 回答
-1

一个非常简单的技巧,只需对您的代码进行 2 次(好的,也许是 3 次)小改动:

ss = ' '
for word in ['this','his','is','s']:
    if ' '+word+' ' not in ss:
        ss = ss + word + ' '
result= ss[1:]

这甚至适用于限制情况 "word" == '',它可以在结果中标识为两个连续的空格(或只有一个开头的空格)。

解释

为了视觉清晰,我将使用':'而不是空格。第一项的处理'this'是微不足道的,结果ss== ':this:'':his:'将找不到下一项,并附加到=='his:'的结果。等等。ss':this:his:'

于 2013-08-14T06:30:31.903 回答