질문 제목을 적절하게 입력했는지 확실하지 않습니다. 그러나 아래의 문제를 설명하려고 노력했습니다. 이 문제를 생각할 수있는 경우 적절한 제목을 제안하십시오. 나는 list in the header
과 attribute in the attri_values
의 일치를 기반으로 테이블을 만들려고 노력하고두 목록 간의 일치를 찾고 일치 항목을 기반으로 출력을 작성하는 방법은 무엇입니까?
list_headers = ['gene_id', 'gene_name', 'trans_id']
# these are the features to be mined from each line of `attri_values`
attri_values =
['gene_id "scaffold_200001.1"', 'gene_version "1"', 'gene_source "jgi"', 'gene_biotype "protein_coding"']
['gene_id "scaffold_200001.1"', 'gene_version "1"', 'trans_id "scaffold_200001.1"', 'transcript_version "1"', 'exon_number "1"', 'gene_source "jgi"', 'gene_biotype "protein_coding"', 'transcript_source "jgi"', 'transcript_biotype "protein_coding"', 'exon_id "scaffold_200001.1.exon1"', 'exon_version "1"']
['gene_id "scaffold_200002.1"', 'gene_version "1"', 'trans_id "scaffold_200002.1"', 'transcript_version "1"', 'exon_number "3"', 'gene_source "jgi"', 'gene_biotype "protein_coding"', 'transcript_source "jgi"', 'transcript_biotype "protein_coding"', 'exon_id "scaffold_200002.1.exon3"', 'exon_version "1"']
:
내가 목록 두 가지 유형의 데이터를 말해봐.
output = open('gtf_table', 'w')
output.write('\t'.join(list_headers) + '\n') # this will first write the header
# then I want to read each line
for values in attri_values:
for list in list_headers:
if values.startswith(list):
attr_id = ''.join([x for x in attri_values if list in x])
attr_id = attr_id.replace('"', '').split(' ')[1]
output.write('\t' + '\t'.join([attr_id]))
elif not values.startswith(list):
attr_id = 'NA'
output.write('\t' + '\t'.join([attr_id]))
output.write('\n')
문제 :이 list of list_headers
에서 일치하는 문자열이 values of attri_values
에서 발견되면 모든 것이 잘 작동하지만, 일치가없는 경우 반복 'NA'가 많이있다.
최종 예상 결과 :
gene_id gene_name trans_id
scaffold_200001.1 NA NA
scaffold_200001.1 NA scaffold_200001.1
scaffold_200002.1 NA scaffold_200002.1
포스트 편집 : 내가 쓴 방법에 이 문제를 내 elif
(때문에 모든이가 'NA'을 쓸 것 이외의 일치를 위해).NA
의 상태를 다른 방법으로 옮기려고했지만 성공하지 못했습니다. 나는 elif
을 제거 경우는 (NA
가 손실)로 출력 번째 얻을 :
gene_id gene_name trans_id
scaffold_200001.1
scaffold_200001.1 scaffold_200001.1
scaffold_200002.1 scaffold_200002.1
답변 해 주셔서 감사합니다. 사전을 사용하면 큰 데이터의 일부에 지나지 않기 때문에 복잡합니다. 간단한 for-loop로 해결할 수 있다고 생각했습니다. Btw,'result = searchHeader (list_headers, attri_values)'에 유형 오류가 발생했습니다. – everestial007
@ everestial007 나쁜 소식! 나는 함수에'list_headers' 대신'title'을 넘겨 줘야했습니다 :'result = searchHeader (title, attri_values)'. 늦은 밤에 코드 작성의 결과가 될 수 있습니까? P? – direprobs
나는 컴퓨터에 너무 많은 정보를 보내거나 졸린 결과를 이해합니다. Btw, 코드는 여전히 나를 위해 문제를 해결할 수 없습니다. 나는 x와 같은 뭔가를 바꾸려고 시도했다.'x에있는 제목 :'나는 그것이''x.startswith (title)'이면 모든 문자열이 일치하지 않는 한 비교 목록에 히트가 없을 이유가 있어야한다고 생각한다. *. 나도 다른 일들을 바꿔 보려고했지만 행운은 없었어. 나에게 완벽한 예제를 주시겠습니까? - 그렇게 될 수 있습니다. 이 질문에 너무 많은 관심을 너무 upvote하시기 바랍니다. – everestial007