Python:交换列表中的两个字符串

Python:交换列表中的两个字符串,python,python-3.x,list,Python,Python 3.x,List,好的,所以我在列表中用另一个字符串的一部分交换字符串的一部分时遇到了问题。我在这个网站上看到了其他类似的问题,但似乎没有一个适合我 假设我有一个列表: sentList = ['i like math', 'i am a cs expert'] 现在我想使用由用户输入确定的变量将“math”与“cs”切换 currentWord = input('Swap word: ') newWord = input('with word: ') 那么,现在我如何交换这两个,以便我的列表将返回以下内容:

好的,所以我在列表中用另一个字符串的一部分交换字符串的一部分时遇到了问题。我在这个网站上看到了其他类似的问题,但似乎没有一个适合我

假设我有一个列表:

sentList = ['i like math', 'i am a cs expert']
现在我想使用由用户输入确定的变量将“math”与“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]

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

假设每个句子只包含一个单词(一个句子不能同时包含
math
cs
),只需创建一个新列表并向其添加新字符串:

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))

下面的代码可以工作。我使用word1(currentWord)和word2(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)
你可以这样试试

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']
这是
列表理解
if-else
条件的简单组合。

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

wa=input....
wb=input....
new = [s.replace(wa,"_").replace(wb,wa).replace("_",wb) for s in sentList]
通过这种方式,一个同时包含这两个词的句子将按以下方式转换:


“cs比数学早”-->“\uI比数学早”-->“\uI比cs早”-->“数学比cs早”

您可以通过简单的交换函数实现它。不需要使代码复杂化:)


使用
列表理解的单个行程序

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

这里,
if sent.find('math')>=0
将查找字符串是否包含
'math'
,如果包含,则将其替换为
'cs'
,否则将
'cs'
替换为
'math'
。如果字符串两者都不包含,那么它也将打印原件,因为只有找到子字符串时,“替换”才起作用


编辑:如前所述,上述代码中存在一些错误。下面是新代码,它将处理所有情况

我使用了
re.sub
来处理只替换单词的问题,替换算法是如何交换两个变量,比如
x
y
,其中我们引入了一个临时变量
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

这个问题有很多模棱两可之处。这里有很多可能的场景,比如@Delgan提到的场景。你能详细介绍一下
s.replace
链接吗?这是如何工作的?请注意,这将把
“数学cs”
变成
“数学cs”
。它还将把
“数学”
转化为
“csematics”
@DakotaBrown,正如Rawing所指出的,有一个轻微的逻辑错误。对于简单的使用,这是好的,但对于复杂的情况,这是失败的。我正在制定一个更好的逻辑。几天后将再次更新@DakotaBrownI请看,请告诉我您何时更新。@DakotaBrown,查看新的和改进的版本:d这将把
“math cs”
变成
“math cs”
“math”
变成
“csematics”
。我最后使用了另一个版本,但效果不错。谢谢你,我感谢你的帮助!这在学习方面确实很有用,但我不允许为此使用函数。
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