gpt4 book ai didi

python - 如何在Python中将二进制元素的数据解析为列表列表?

转载 作者:行者123 更新时间:2023-12-01 03:35:05 24 4
gpt4 key购买 nike

示例如下所示:

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:[[], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

我是解析数据并试图找出正确方法的新手。关于如何修改代码的建议以及解释表示赞赏。

最佳答案

我会通过以下方式做到这一点:

import re

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']

result = []
curr_group = []
for item in lst:
item = item.rstrip() # Remove \n
if '$$' in item:
if len(curr_group) > 0: # Check to see if binary numbers have been found.
result.append(curr_group)
curr_group = []
elif re.match('[01]+$', item): # Checks to see if string is binary (0s or 1s).
curr_group.append(item)

result.append(curr_group) # Appends final group due to lack of ending '$$'.

print(result)

基本上,您希望迭代这些项目,直到找到 '$$',然后将之前找到的任何二进制字符添加到最终结果中,并开始一个新组。您找到的每个二进制字符串(使用正则表达式)都应添加到当前组中。

最后,您需要添加最后一组二进制数,因为没有尾随 '$$'

关于python - 如何在Python中将二进制元素的数据解析为列表列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40458350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com