2017-09-26 385 views
3

好的,所以我有一个问题交换部分字符串与列表中的另一个字符串的一部分。我在这个网站上看到了其他类似的问题,但它们都不适合我。Python:交换列表中的两个字符串

比方说,我有一个列表:

sentList = ['i like math', 'i am a cs expert'] 

现在我想改用“数学”与“CS”使用由用户输入确定的变量。

currentWord = input('Swap word: ') 
newWord = input('with word: ') 

所以,现在我将如何交换两个让我的名单将返回以下:

sentList = ['i like cs', 'i am a math expert'] 

和往常一样,感谢你的帮助。我想我可以使用替换功能,但不知道如何。我想这将是这个样子:

sentList = [sentListN.replace(currentWord, newWord) for sentListN in sentList] 

但是,显然这是行不通的。

+1

地块。在这里可能有很多场景,就像@Delgan所说的那样。 –

回答

1

单线程使用list comprehension

[sent.replace(currentWord,newWord) if sent.find(currentWord)>=0 else sent.replace(newWord,currentWord) for sent in sentList] 

所以,

IN: sentList = ['i like math', 'i am a cs expert'] 
IN : currentWord = "math" 
IN : newWord = "cs" 
OUT : ['i like cs', 'i am a math expert'] 

这里,if sent.find('math')>=0会发现,如果字符串conntains 'math',如果是这样,与'cs'替换它,否则它取代'cs''math'。如果字符串不包含,那么它也会打印原件,因为只有在找到子字符串时,替换才会有效。


编辑:作为@Rawing指出,有在上面的代码中的一些错误。所以这里是处理所有情况的新代码。

我已经使用re.sub处理更换只有文字,而替换算法是,你将如何交换两个变量,譬如说xy,在这里我们引入一个临时变量t,以帮助交换: t = x; x = y; y = t。由于我们必须这样做,所以选择了这种方法。同时多次替换

from re import sub 

for s in sentList: 

    #replace 'math' with '%math_temp%' in s (here '%math_temp%' is a dummy placeholder that we need to later substitute with 'cs') 
    temp1 = sub(r'\bmath\b','%math_temp%' , s) 

    #replace 'cs' with 'math' in temp1 
    temp2 = sub(r'\bcs\b','math', temp1) 

    #replace '%math_temp%' with 'cs' in temp2 
    s = sub('%math_temp%','cs', temp2) 

    print(s) 

所以这次是更大的考验时,我们得到:在这个问题含糊不清的

IN : sentList = ['i like math', 'i am a cs expert', 'math cs', 'mathematics'] 
IN : currentWord = "math" 
IN : newWord = "cs" 

OUT : i like cs 
     i am a math expert 
     cs math 
     mathematics 
+1

请注意,这会将数学cs转换为cs cs。它也会把''数学''变成''csematics'''。 –

+0

嗯。真@Rawing。让我改变这一点。感谢您的单挑 –

+1

我最终使用了这个,谢谢! –

0

假设每个句子只包含一个字(一个句子不能同时包含mathcs)只是做一个新的列表和新的字符串追加到它:

newSentList = [] 
for sent in sentList: 
    assert(currentWord in sent != newWord in sent) 
    if currentWord in sent: 
     newSentList.append(sent.replace(currentWord, newWord)) 
    else: 
     newSentList.append(sent.replace(newWord, currentWord)) 
1

下面的代码工作。我用字1(currentWord)和单词2(newWord)变量作为用户输入

sentList = ['i like math', 'i am a cs expert'] 

word1 = 'math' 
word2 = 'cs' 

assert word1, word2 in sentList 
sentList = [s.replace(word1, word2) if word1 in s 
      else s.replace(word2, word1) if word2 in s 
      else s for s in sentList] 

如果我们打破它的步骤,它看起来像

for i, s in enumerate(sentList): 
    if word1 in s: 
     sentList[i] = s.replace(word1, word2) 
    elif word2 in s: 
     sentList[i] = s.replace(word2, word1) 
+0

这将把''数学cs''变成''cs cs'''和''数学''变成''csematics'''。 –

+0

我结束了使用另一个,但这工作。谢谢,我感谢你的帮助! –

0

你可以尝试这样的,

In [20]: currentWord = 'cs' 

In [21]: newWord = 'math' 

In [22]: [i.replace(currentWord,newWord) if currentWord in i else i.replace(newWord,currentWord) if newWord in i else i for i in sentList] 
Out[22]: ['i like cs', 'i am a math expert'] 

这是list comprehensionif else条件的简单组合。

0

这是一个天真的答案(字可以是其他词等子),但按照你的方法,我们可以通过

wa=input.... 
wb=input.... 
new = [s.replace(wa,"_").replace(wb,wa).replace("_",wb) for s in sentList] 

wb交换wa这样既句子转化以下方法:

“CS是老年人则数学” - > “_是老年人则数学” - > “_是老年人则CS” - > “数学是老年人则CS”

+0

你可以详细说明's.replace'链吗?这个怎么用? – Vinny

+0

@Vinny,请看我的编辑是否有意义。 – JJoao

1

可以实现它通过简单的交换功能。不需要复杂的代码:)

def swap(str, x, y): 
    words = [] 
    for w in str.split(): 
     if w == x: 
      words.append(y) 
     elif w == y: 
      words.append(x) 
     else: 
      words.append(w) 
    return ' '.join(words) 

if __name__ == '__main__': 
    sentList = ['i like math', 'i am a cs expert'] 
    word1 = 'math' 
    word2 = 'cs' 
    new_list = [swap(x, word1, word2) for x in sentList] 
    print(new_list) 
+0

这实际上在学习方面非常有用,但我不允许为此使用函数。 –

相关问题