将此列表(如不带逗号的字符串)转换为list - python

我有这个字符串:

my_string = "[a:b][c:d][e:f]"

我想将其转换为这样的列表:-

my_list = ['[a:b]','[c:d]','[e:f]']

请注意,列表中的元素仅仅是字符串。
但是我尝试了这些:
ast.literal_eval(my_string)map(int,mystring)

他们两个都返回错误。
拆分命令还返回以下内容:['[a:b] [c:d]']。有人可以帮帮我吗?提前致谢。

python大神给出的解决方案

您可以使用re.findall,似乎效率更高:

my_string = "[a:b][c:d][e:f]"

import re
re.findall("(\[.*?\])",my_string)

输出:

In [93]: r = re.compile("(\[.*?\])")

In [94]: my_string = "[a:b][c:d][e:f]"

In [95]: r.findall(my_string)
Out[95]: ['[a:b]', '[c:d]', '[e:f]']

或使用str.replace在开头和结尾之间放置一个空格,然后拆分:

In [101]: my_string = "[a:b][c:d][e:f]"

In [102]: my_string.replace("][","] [").split()
Out[102]: ['[a:b]', '[c:d]', '[e:f]']

哪个是最有效的:

In [103]: timeit my_string.replace("][","] [").split()
1000000 loops, best of 3: 445 ns per loop

In [104]: %%timeit         
s = '[a:b][c:d][e:f]'
l = s.split(']')
for i in xrange(len(l)):
    l[i] += ']'
del l[len(l)-1] #Th
   .....: 
1000000 loops, best of 3: 1.04 µs per loop

In [105]: timeit r.findall(my_string)
1000000 loops, best of 3: 962 ns per loop