我想知道将由空格分隔的字符串拆分为字符串中的最后一个空格的最佳方法,该空格不在
[, {, ( or "
内。例如,我可能有:a = 'a b c d e f "something else here"'
b = 'another parse option {(["gets confusing"])}'
对于 a,它应该解析为['a','b','c','d','e','f'], ["something else here"]
而 b 应解析为 ['another','parse','option'],['([{"gets confusing"}])']
现在我有这个:
def getMin(aList):
min = sys.maxint
for item in aList:
if item < min and item != -1:
min = item
return min
myList = []
myList.append(b.find('['))
myList.append(b.find('{'))
myList.append(b.find('('))
myList.append(b.find('"'))
myMin = getMin(myList)
print b[:myMin], b[myMin:]
我相信有更好的方法来做这件事,也欢迎各种建议。