样品看起来是这样的:如何使用二进制元素将数据解析到Python列表中?
lst = ['ms 20 3 -s 10 \n', '17954 11302 58011\n', '\n', '$$\n', 'segsites: 10\n', 'positions: 0.0706 0.2241 0.2575 0.889 \n', '0001000010\n', '0101000010\n', '0101010010\n', '0001000010\n', '\n', '$$\n', 'segsites: 10\n', 'positions: 0.0038 0.1622 0.1972 \n', '0110000110\n', '1001001000\n', '0010000110\n', '$$\n', 'segsites: 10\n', 'positions: 0.0155 0.0779 0.2092 \n', '0000001011\n', '0000001011\n', '0000001011\n']
每一个新的集合与$$开始。我需要解析数据,以便我列出以下列表。
sample = [['0001000010', '0101000010', '0101010010', '0001000010'],['0110000110', '1001001000', '0010000110'],['0000001011', '0000001011', '0000001011'] # Required Output
代码在分析数据,并试图找出如何得到这个权利试图
sample =[[]]
sample1 = ""
seqlist = []
for line in lst:
if line.startswith("$$"):
if line in '01': #Line contains only 0's or 1
sample1.append(line) #Append each line that with 1 and 0's in a string one after another
sample.append(sample1.strip()) #Do this or last line is lost
print sample
Output:[[], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
我是一个新手。赞赏如何修改代码和解释的建议。
我似乎有麻烦使它成为我的原始数据,虽然设置工作。 https://eval.in/673188 – biogeek
在我的原始数据集中,分隔符($$)是不同的。一旦我改变分隔符的类型,输出就会崩溃。 – biogeek
有什么建议吗? – biogeek