我有一个列表= ['0.2 0.2 0.3 0.2 0.3','0.4 0.3 0.1 0.5 0.1','0.3 0.3 0.5 0.2 0.4','0.1 0.2 0.1 0.1 0.2'],我需要用逗号分隔像这样的元素: '0.2,0.2,0.3,0.2,0.3'... 我正在解析文件,但在此步骤中卡住了。
我已经尝试过了:
with open('profileKmer.txt') as f:
lines = f.read().splitlines()
prof = [x.strip(' ') for x in lines[4:8]]
profile = []
for element in prof:
if element.startswith('0'):
profile.extend(element.split(','))
print(profile)
但是我没有得到想要的东西。
数据如下:
headline
input
ACCTGTTTATTGCCTAAGTTCCGAACAAACCCAATATAGCCCGAGGGCCT
5
0.2 0.2 0.3 0.2 0.3
0.4 0.3 0.1 0.5 0.1
0.3 0.3 0.5 0.2 0.4
0.1 0.2 0.1 0.1 0.2
我真的很感谢任何见识。
答案 0 :(得分:0)
您可以尝试以下方法:
def trial(list):
new_list = []
for item in list:
temp_item = ",".join(item.split())
new_list.append(temp_item)
print(new_list)
# Result:
# ['0.2,0.2,0.3,0.2,0.3', '0.4,0.3,0.1,0.5,0.1', '0.3,0.3,0.5,0.2,0.4', '0.1,0.2,0.1,0.1,0.2']
if __name__ == '__main__':
list = ['0.2 0.2 0.3 0.2 0.3', '0.4 0.3 0.1 0.5 0.1', '0.3 0.3 0.5 0.2 0.4', '0.1 0.2 0.1 0.1 0.2']
trial(list)