你如何在python中划分一个字符串?

时间:2015-03-31 00:56:52

标签: python

我有一个字符串形式为(+ m1 +" |" + m2 +)和(+ m1 +"。" + m2 +) 其中m1和m2是由" 6"," 7"," 8"," a"组成的字符串。 这些是一些有效的表达式:

"(6|7)"
"((8.7)|(6.7).(a.2))"

现在我的问题是,如果我想分开"。"基本上这是一个除数,我该怎么做?

我所做的是尝试找到一个中间点,但事情是它并不总是在中间 我也试过做s.rindex("。")和s.index("。")和s.find("。")但是他们也似乎不起作用。

我正在考虑调用最外面的括号,然后在内部工作 但我不认为它会起作用。

我在想与括号有一些关系,但我似乎无法弄清楚它是什么。 关于如何解决这个问题的任何建议?或暗示我如何找到分裂点? 任何帮助将不胜感激。 提前致谢

1 个答案:

答案 0 :(得分:0)

这是我对你所要求的解释。如果没有更多信息,明确陈述的问题,预期的输出或显示您自己尝试的代码,我就无法做更多。

test1 = "(6|7)"
test2 = "((8.7)|(6.7).(a.2))"
# Not really sure what your output is suppose to look
# Like so this is my interpretation 
test1_split = test1.replace("(", "").replace(")", "").split("|")
print test1_split # output --> ['6', '7'] 
                  # creates a list containing the string 6 and 7
# You said the . works an a divisor.  If you mean a delimiter then you can use the split("."") method
# If you mean that it is a symbol for a divide sign then see below
test2 = test2.replace(".", "/") # convert periods into dividing sign
test2_split = test2.split("|")  # seperates (8/7) (6/7)/(a/2))
print test2_split # output ['((8/7)', '(6/7)/(a/2))']

# now if this is an equation I would continue...Since I don't know the output
# I leave the rest to you