테스트 사이트 - 개발 중인 베타 버전입니다

파이썬 관련 질문입니다.

음 input1.txt 파일과 input2.txt 파일이 있는데요

 

input1.txt ------------------

chr1     a1     b1     c1

chr2     a2     b2     c2

chr3     a3     b3     c3

   .         .        .        .

   .         .        .        .

chr10   a10    b10   c10

--------------------------

 

 

input2.txt ------------------

chr1     d1     e1     f1

chr11   d2     e2     f2

chr2     d3     e3     f3

chr22   d4     e4     f4

. ...

    .        .        .       .

chr55   d10   e10   f10

---------------------------

 

각 파일의 내용은 이렇습니다.

 

제가 하고자 하는 작업은

 

두개의 파일의 배열[0]값을 서로 비교해서

같은값만을 출력하고 나머지

input1의 배열[1]과, 위에서 출력한 값에 해당하는 input2의 배열 [2],[3]을 차례대로 출력하고자 하는것인데요.

 

그러니까

 

output ---------------------------

chr1     a1     e1     f1

chr2     a2     e3     f3

chr3     a3     e5     f5

... .

   .         .        .       .

chr10   a10    e10   f10

---------------------------------

 

결과는 이렇게 나와야겠죠

 

 

 

이거는 제가 해본건데요.

그냥 출력하는것밖에 못했습니다..

---------------------------------

f=open("input1.txt")

q=open("input2.txt')

 

for line in f:

           index1=line.strip().split("     ");

for line in q:

           index2=line.strip().split("     ");

 

           print index1[0], index1[1],index2[0],index2[3];

 

f.close();

------------------------------------

 

if문을 써서 index1[0] 값과 index2[0]값을 비교해서

같으면 같은값만 출력하게하면 될거같은데..... 잘모르겠네요

 

어떻게 해야될까요??

 

고수분들의 도움 부탁드립니다..

 

 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 2개

방법은 여러가지가 있는데, 간단히 이런 식으로 해볼 수 있겠죠.

def table(lines):
def f(x): return (x[0], x[1:])
return dict(f(line.split()) for line in lines)

data1 = table(open('input1.txt').readlines())
data2 = table(open('input2.txt').readlines())

for key in sorted(set(data1) & set(data2)):
print key, data1[key][0], data2[key][1], data2[key][2]

게시글 목록

번호 제목
8050
8048
19969
19968
19967
19966
19965
19964
19963
19962
19961
28345
31017
19960
19959
19958
19957
8039
8035
8029
28344
28339
8019
28338
8017
8010
8007
8004
8003
28332
28322
7999
28317
20956
7992
20945
28314
20936
20931
7986
20925
7982
7979
7978
7975
28307
7973
7966
28305
7963
28300
7961
28297
28295
7959
19948
19947
28292
31016
19946
19945
28286
7958
7956
7952
7946
28285
28283
19943
7944
7936
7931
28279
24666
24663
7928
7923
19941
28274
28257
28249
28248
7914
7912
7911
20912
7908
31755
28233
28232
28228
7903
28217
20900
24662
20891
20882
19936
20853
31011