Python 如何减去两个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18454570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to subtract two strings?
提问by max smith
I have a long string, which is basically a list like str="lamp, bag, mirror,"
(and other items)
我有一个很长的字符串,它基本上是一个类似str="lamp, bag, mirror,"
(和其他项目)的列表
I was wondering if I can add or subtract some items, in other programming languages I can easily do: str=str-"bag,"
and get str="lamp, mirror,"
this doesnt work in python (I'm using 2.7 on a W8 pc)
我想知道我是否可以添加或减去一些项目,在其他编程语言中我可以轻松做到:str=str-"bag,"
并且str="lamp, mirror,"
在 python 中不起作用(我在 W8 pc 上使用 2.7)
Is there a way to split the string across say "bag," and somehow use that as a subtraction? Then I still need to figure out how to add.
有没有办法将字符串拆分为“包”,然后以某种方式将其用作减法?然后我还需要弄清楚如何添加。
回答by óscar López
You can do this as long as you use well-formed lists:
只要您使用格式良好的列表,就可以执行此操作:
s0 = "lamp, bag, mirror"
s = s0.split(", ") # s is a list: ["lamp", "bag", "mirror"]
If the list is not well-formed, you can do as follows, as suggested by @Lattyware:
如果列表格式不正确,您可以按照@Lattyware 的建议执行以下操作:
s = [item.strip() for item in s0.split(',')]
Now to delete the element:
现在删除元素:
s.remove("bag")
s
=> ["lamp", "mirror"]
Either way - to reconstruct the string, use join()
:
无论哪种方式 - 要重建字符串,请使用join()
:
", ".join(s)
=> "lamp, mirror"
A different approach would be to use replace()
- but be careful with the string you want to replace, for example "mirror"
doesn't have a trailing ,
at the end.
一种不同的方法是使用replace()
- 但要小心要替换的字符串,例如"mirror"
末尾没有尾随,
。
s0 = "lamp, bag, mirror"
s0.replace("bag, ", "")
=> "lamp, mirror"
回答by dare
you should convert your string to a list of string then do what you want. look
您应该将字符串转换为字符串列表,然后执行您想要的操作。看
my_list="lamp, bag, mirror".split(',')
my_list.remove('bag')
my_str = ",".join(my_list)
回答by Joran Beasley
you could also just do
你也可以这样做
print "lamp, bag, mirror".replace("bag,","")
回答by badc0re
Using regular expression example:
使用正则表达式示例:
import re
text = "lamp, bag, mirror"
word = "bag"
pattern = re.compile("[^\w]+")
result = pattern.split(text)
result.remove(word)
print ", ".join(result)
回答by Zaka Elab
How about this?:
这个怎么样?:
def substract(a, b):
return "".join(a.rsplit(b))
回答by Rohan Amrute
If you have two strings like below:
如果你有两个像下面这样的字符串:
t1 = 'how are you'
t2 = 'How is he'
and you want to subtract these two strings then you can use the below code:
如果您想减去这两个字符串,则可以使用以下代码:
l1 = t1.lower().split()
l2 = t2.lower().split()
s1 = ""
s2 = ""
for i in l1:
if i not in l2:
s1 = s1 + " " + i
for j in l2:
if j not in l1:
s2 = s2 + " " + j
new = s1 + " " + s2
print new
Output will be like:
输出将类似于:
are you is he
你是他吗
回答by J. Doe
Using the following you can add more words to remove (["bag", "mirror", ...]
)
使用以下内容,您可以添加更多单词以删除 ( ["bag", "mirror", ...]
)
(s0, to_remove) = ("lamp, bag, mirror", ["bag"])
s0 = ", ".join([x for x in s0.split(", ") if x not in to_remove])
=> "lamp, mirror"
回答by Dengs
from re import sub
def Str2MinusStr1 (str1, str2, n=1) :
return sub(r'%s' % (str2), '', str1, n)
Str2MinusStr1 ('aabbaa', 'a')
# result 'abbaa'
Str2MinusStr1 ('aabbaa', 'ab')
# result 'abaa'
Str2MinusStr1 ('aabbaa', 'a', 0)
# result 'bb'
# n = number of occurences.
# 0 means all, else means n of occurences.
# str2 can be any regular expression.